From 6d2c566caf09d2c0433d12b725a4e851d7d1dc05 Mon Sep 17 00:00:00 2001 From: rakesh Date: Wed, 22 Jan 2020 19:00:55 +0530 Subject: [PATCH] Python Tools --- Tools/csv_to_json.py | 8 +++++--- Tools/csv_to_xml.py | 23 +++++++++++++++++++++++ Tools/sample.xml | 1 + Tools/xml_beatifier.py | 3 +++ dictionary/dict_example.py | 3 +++ fileHandling/csv_to_json_converter.py | 27 ++++++++++++--------------- jsonFile.json | 2 +- 7 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 Tools/csv_to_xml.py create mode 100644 Tools/sample.xml create mode 100644 Tools/xml_beatifier.py create mode 100644 dictionary/dict_example.py diff --git a/Tools/csv_to_json.py b/Tools/csv_to_json.py index 7e71e9a..1530efe 100644 --- a/Tools/csv_to_json.py +++ b/Tools/csv_to_json.py @@ -1,5 +1,7 @@ import csv -json = [] +import json + +data = [] keys = [] with open("E:/python/Tools/SampleData.csv") as csv_file: csv_data = csv.reader(csv_file) @@ -12,8 +14,8 @@ element = {} for y in range(len(row)): element[keys[y]] = row[y] - json.append(element) + data.append(element) count = count+1 -print(json) +print(json.dumps(data,)) diff --git a/Tools/csv_to_xml.py b/Tools/csv_to_xml.py new file mode 100644 index 0000000..c48ebf3 --- /dev/null +++ b/Tools/csv_to_xml.py @@ -0,0 +1,23 @@ +# python program to convert any csv file into xml file +# made by : rakesh kumar + +import csv +xml = '' +keys = [] +with open("E:/python/Tools/SampleData.csv") as csv_file: + csv_data = csv.reader(csv_file) + count = 1 + for row in csv_data: + + if count == 1: + for y in row: + keys.append(y) + else: + xml += "" + for y in range(len(row)): + xml += "<"+keys[y]+">"+row[y]+"" + xml += "" + count += 1 + +xml += "" +print(xml) diff --git a/Tools/sample.xml b/Tools/sample.xml new file mode 100644 index 0000000..d54ac66 --- /dev/null +++ b/Tools/sample.xml @@ -0,0 +1 @@ +1Wii SportsWii2006SportsNintendo41.4929.023.778.4682.742Super Mario Bros.NES1985PlatformNintendo29.083.586.810.7740.243Mario Kart WiiWii2008RacingNintendo15.8512.883.793.3135.824Wii Sports ResortWii2009SportsNintendo15.7511.013.282.96335Pokemon Red/Pokemon BlueGB1996Role-PlayingNintendo11.278.8910.22131.376TetrisGB1989PuzzleNintendo23.22.264.220.5830.267New Super Mario Bros.DS2006PlatformNintendo11.389.236.52.930.018Wii PlayWii2006MiscNintendo14.039.22.932.8529.029New Super Mario Bros. WiiWii2009PlatformNintendo14.597.064.72.2628.6210Duck HuntNES1984ShooterNintendo26.930.630.280.4728.3111NintendogsDS2005SimulationNintendo9.07111.932.7524.7612Mario Kart DSDS2005RacingNintendo9.817.574.131.9223.4213Pokemon Gold/Pokemon SilverGB1999Role-PlayingNintendo96.187.20.7123.114Wii FitWii2007SportsNintendo8.948.033.62.1522.7215Wii Fit PlusWii2009SportsNintendo9.098.592.531.7922 \ No newline at end of file diff --git a/Tools/xml_beatifier.py b/Tools/xml_beatifier.py new file mode 100644 index 0000000..14b565d --- /dev/null +++ b/Tools/xml_beatifier.py @@ -0,0 +1,3 @@ +with open("E:/python/Tools/sample.xml", "r") as file: + data = file.read() + print(data) diff --git a/dictionary/dict_example.py b/dictionary/dict_example.py new file mode 100644 index 0000000..5e598e2 --- /dev/null +++ b/dictionary/dict_example.py @@ -0,0 +1,3 @@ + +d1 = {"name": "rakesh"} +print(d1) diff --git a/fileHandling/csv_to_json_converter.py b/fileHandling/csv_to_json_converter.py index e1231be..ed97422 100644 --- a/fileHandling/csv_to_json_converter.py +++ b/fileHandling/csv_to_json_converter.py @@ -1,26 +1,23 @@ import csv import json -with open(r'C:/Users/rakesh\Desktop/python/fileHandling/convert.csv') as csf_file: +with open('e:/python/tools/SampleData.csv') as csf_file: csv_reader = csv.reader(csf_file) - line=0 - keys=[] - jon=[] + line = 0 + keys = [] + jon = [] for row in csv_reader: d = {} - if line==0: + if line == 0: for col in row: keys.append(col) else: for i in range(len(row)): - d[keys[i]]=row[i] - - if(line>=1): - jon.append(d) - line+=1 -#print(jon) - -with open("jsonFile.json","w") as json_file: - json_file.write( json.dumps(jon,)) + d[keys[i]] = row[i] + if(line >= 1): + jon.append(d) + line += 1 +# print(jon) - +with open("jsonFile.json", "w") as json_file: + json_file.write(json.dumps(jon,)) diff --git a/jsonFile.json b/jsonFile.json index 9f3257a..03eac15 100644 --- a/jsonFile.json +++ b/jsonFile.json @@ -1 +1 @@ -[{"album": "The White Stripes", " year": "1999", " US_peak_chart_post": " -"}, {"album": "De Stijl", " year": "2000", " US_peak_chart_post": " -"}, {"album": "White Blood Cells", " year": "20.01", " US_peak_chart_post": "61"}, {"album": "Elephant", " year": "2003", " US_peak_chart_post": "6"}, {"album": "Get Behind Me Satan", " year": "2005", " US_peak_chart_post": "3"}, {"album": "Icky Thump", " year": "2007", " US_peak_chart_post": "2"}, {"album": "Under Great White Northern Lights", " year": "2010", " US_peak_chart_post": "11"}, {"album": "Live in Mississippi", " year": "2011", " US_peak_chart_post": " -"}, {"album": "Live at the Gold Dollar", " year": "2012", " US_peak_chart_post": " -"}, {"album": "Nine Miles from the White City", " year": "2013", " US_peak_chart_post": " -"}] \ No newline at end of file +[{"Rank": "1", "Name": "Wii Sports", "Platform": "Wii", "Year": "2006", "Genre": "Sports", "Publisher": "Nintendo", "NA_Sales": "41.49", "EU_Sales": "29.02", "JP_Sales": "3.77", "Other_Sales": "8.46", "Global_Sales": "82.74"}, {"Rank": "2", "Name": "Super Mario Bros.", "Platform": "NES", "Year": "1985", "Genre": "Platform", "Publisher": "Nintendo", "NA_Sales": "29.08", "EU_Sales": "3.58", "JP_Sales": "6.81", "Other_Sales": "0.77", "Global_Sales": "40.24"}, {"Rank": "3", "Name": "Mario Kart Wii", "Platform": "Wii", "Year": "2008", "Genre": "Racing", "Publisher": "Nintendo", "NA_Sales": "15.85", "EU_Sales": "12.88", "JP_Sales": "3.79", "Other_Sales": "3.31", "Global_Sales": "35.82"}, {"Rank": "4", "Name": "Wii Sports Resort", "Platform": "Wii", "Year": "2009", "Genre": "Sports", "Publisher": "Nintendo", "NA_Sales": "15.75", "EU_Sales": "11.01", "JP_Sales": "3.28", "Other_Sales": "2.96", "Global_Sales": "33"}, {"Rank": "5", "Name": "Pokemon Red/Pokemon Blue", "Platform": "GB", "Year": "1996", "Genre": "Role-Playing", "Publisher": "Nintendo", "NA_Sales": "11.27", "EU_Sales": "8.89", "JP_Sales": "10.22", "Other_Sales": "1", "Global_Sales": "31.37"}, {"Rank": "6", "Name": "Tetris", "Platform": "GB", "Year": "1989", "Genre": "Puzzle", "Publisher": "Nintendo", "NA_Sales": "23.2", "EU_Sales": "2.26", "JP_Sales": "4.22", "Other_Sales": "0.58", "Global_Sales": "30.26"}, {"Rank": "7", "Name": "New Super Mario Bros.", "Platform": "DS", "Year": "2006", "Genre": "Platform", "Publisher": "Nintendo", "NA_Sales": "11.38", "EU_Sales": "9.23", "JP_Sales": "6.5", "Other_Sales": "2.9", "Global_Sales": "30.01"}, {"Rank": "8", "Name": "Wii Play", "Platform": "Wii", "Year": "2006", "Genre": "Misc", "Publisher": "Nintendo", "NA_Sales": "14.03", "EU_Sales": "9.2", "JP_Sales": "2.93", "Other_Sales": "2.85", "Global_Sales": "29.02"}, {"Rank": "9", "Name": "New Super Mario Bros. Wii", "Platform": "Wii", "Year": "2009", "Genre": "Platform", "Publisher": "Nintendo", "NA_Sales": "14.59", "EU_Sales": "7.06", "JP_Sales": "4.7", "Other_Sales": "2.26", "Global_Sales": "28.62"}, {"Rank": "10", "Name": "Duck Hunt", "Platform": "NES", "Year": "1984", "Genre": "Shooter", "Publisher": "Nintendo", "NA_Sales": "26.93", "EU_Sales": "0.63", "JP_Sales": "0.28", "Other_Sales": "0.47", "Global_Sales": "28.31"}, {"Rank": "11", "Name": "Nintendogs", "Platform": "DS", "Year": "2005", "Genre": "Simulation", "Publisher": "Nintendo", "NA_Sales": "9.07", "EU_Sales": "11", "JP_Sales": "1.93", "Other_Sales": "2.75", "Global_Sales": "24.76"}, {"Rank": "12", "Name": "Mario Kart DS", "Platform": "DS", "Year": "2005", "Genre": "Racing", "Publisher": "Nintendo", "NA_Sales": "9.81", "EU_Sales": "7.57", "JP_Sales": "4.13", "Other_Sales": "1.92", "Global_Sales": "23.42"}, {"Rank": "13", "Name": "Pokemon Gold/Pokemon Silver", "Platform": "GB", "Year": "1999", "Genre": "Role-Playing", "Publisher": "Nintendo", "NA_Sales": "9", "EU_Sales": "6.18", "JP_Sales": "7.2", "Other_Sales": "0.71", "Global_Sales": "23.1"}, {"Rank": "14", "Name": "Wii Fit", "Platform": "Wii", "Year": "2007", "Genre": "Sports", "Publisher": "Nintendo", "NA_Sales": "8.94", "EU_Sales": "8.03", "JP_Sales": "3.6", "Other_Sales": "2.15", "Global_Sales": "22.72"}, {"Rank": "15", "Name": "Wii Fit Plus", "Platform": "Wii", "Year": "2009", "Genre": "Sports", "Publisher": "Nintendo", "NA_Sales": "9.09", "EU_Sales": "8.59", "JP_Sales": "2.53", "Other_Sales": "1.79", "Global_Sales": "22"}] \ No newline at end of file