def convert_csv_to_json(filename): output_file_name = filename.split('.')[-2] if '/' in output_file_name: output_file_name = output_file_name.split('/')[-1] output_file_name += "_" + str(int(random.random() * 10000)) + ".json" with open(filename) as r, open(output_file_name, 'w') as w: convert(r, w) print("File conversion completed!, output filename= ", output_file_name) return output_file_name
def get(self, request, format=None, **kwargs): # with open('static/json/data.json', newline='') as jsonfile: # data = json.load(jsonfile) # return Response(data) # with open('static/csv/data.csv', newline='') as csvfile: # data = csv.DictReader(csvfile) # return Response(data) with open('static/csv/data.csv') as r, open('static/json/data.json', 'w') as w: convert(r, w) with open('static/json/data.json', newline='') as jsonfile: data = json.load(jsonfile) return Response(data)
from csv2json import convert, load_csv, save_json with open('test2.csv') as r, open('test2.json', 'w') as w: convert(r, w)
print (update_progress(float(i/int(pages)))), print('\n' + Fore.RESET + Back.RESET + Style.RESET_ALL + "\t Append to master csv? [y or n]\n\t " + Fore.GREEN) masterAppend = raw_input('\t ') print Fore.RESET if masterAppend.lower() == "y": if os.path.isfile('hood_roulette_master.csv'): with open('hood_roulette_master.csv', 'a+') as masterfile: appender = csv.writer(masterfile, dialect = 'excel', lineterminator='\n') for val in titles: appender.writerow(val) masterfile.close() else: print Fore.RED + 'Master file not found.' #Function for writing information to CSVfile with open(csvfile, "w+") as csvfile: writer = csv.writer(csvfile, lineterminator='\n') writer.writerow(['title','youtubeId']) for val in titles: writer.writerow(val) csvfile.close() #Function from csv2json.py for turning csv directly into a json file csv2json.convert(csvfile) time.sleep(1) raw_input(Fore.YELLOW + Back.BLUE + "\n\t VICTORY!!!!! -_- \n")
# Requirement: # # Write a module that defines a function that takes two arguments: a path to a # CSV file and a path to a JSON file. # The function must read the CSV file and write to the JSON file. # The JSON file must contain an array of JSON objects (one for each CSV row). # Each object must have the name of the columns as keys and the row values as # its corresponding values. # Also, write a script that uses the created module. # Implemented on Python 3.6. import argparse, csv2json parser = argparse.ArgumentParser(description='CSV to JSON converter.') # Add CSV file argument. parser.add_argument('csv', help='Path to CSV file') # Add JSON file argument. parser.add_argument('json', help='Path to JSON file') # Parse command line arguments. args = parser.parse_args() csv2json.convert(args.csv, args.json)
# This function contains all code which answers Question 2. def analysisQuestion2(): sortedwinsbycountry = winsbycountry(data) plotwinbycountry(sortedwinsbycountry, 'Question2.png') # This function contains all code which answers Question 3. def analysisQuestion3(): countryrecords = getrecords() createdataframe(countryrecords[0], countryrecords[1], countryrecords[2], countryrecords[3]) input_file_name = 'results.csv' output_file_name = 'game_results.json' if not os.path.exists(output_file_name): csv2json.convert(input_file_name, output_file_name) else: print(f'{output_file_name} already exists.') with open(output_file_name) as json_file: data = json.load(json_file) analysisQuestion1("Republic of Ireland") analysisQuestion2() analysisQuestion3()