You may write the JSON String to a JSON file.Convert the Python List to JSON String using json.dumps().Add the dictionary to the Python List created in step 1. Read the lines of CSV file using csv.DictReader() function.To convert CSV to JSON in Python, follow these steps: Since I have more than 2000 rows, it is not a feasible solution.Ĭan anyone suggest how to parse CSV file line by line and convert it into JSON without specifying fieldnames? Answer by Milovan Tomašević Python CSV to JSON write ( ' \n ' )īut the problem with above code is that we need to mention the field names to parse the CSV. DictReader ( csvfile, fieldnames ) for row in reader : json. Import csv import json csvfile = open ( 'file.csv', 'r' ) jsonfile = open ( 'file.json', 'w' ) fieldnames = ( "FirstName", "LastName", "IDNumber", "Message" ) reader = csv.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |