The advantage is that all data is already in the CSV if the program crashes or if you terminate it. Instead of building a huge dataframe over time, the code below appends the fetched data directly into the CSV file. The output of above code: item amount weight price bestbeforeendeateĮDIT: I had another look at the problem and thought I share another solution, which might be better for you. # The dataframe you want to store everythingĭf = add_new_entry_to_dataframe(df, newly_fetched_result) Input_parsed = json.loads(api_result_string)ĭf_with_new_data = pd.json_normalize(input_parsed) # Function converts the api result to the dataframe and appends it to dfĭef add_new_entry_to_dataframe(df, api_result_string): In order to convert a JSON string into a dict you can simply use the json library. JsonString = json.Pandas has a function called json_normalize, which can directly convert a dict into a dataframe. With open(jsonFilePath, 'w', encoding='utf-8') as jsonf: #convert python jsonArray to JSON String and write to file #load csv file data using csv library's dictionary reader With open(csvFilePath, encoding='utf-8-sig') as csvf: But with no luck.ĭef csv_to_json(csvFilePath, jsonFilePath): I have tried looking into specifying seperators flag in the json.dump function. ![]() ![]() Im trying to remove the whitespace from the JSON values, but not sure how to do this. The Python script generates a JSON file (data.json).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |