skip to Main Content

In my Python code, I have a for loop that generates flattened JSON files.
I would like to append these JSON files recursively to a file (for example a .csv file) using the same for loop.

my final .csv should look like this:

{"name": "John", "age": 30, "married": true, "divorced": false, "children": ["Ann", "Billy"], "pets": null, "cars": [{"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1}]}
{"name": "Doe", "age": 33, "married": true, "divorced": false, "children": ["Peter", "Billy"], "pets": null, "cars": [{"model": "Tesla", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 27.1}]}
{"name": "Kurt", "age": 13, "married": true, "divorced": false, "children": ["Bruce", "Nikola"], "pets": null, "cars": [{"model": "Mercedes", "mpg": 27.5}, {"model": "123", "mpg": 24.1}]}

3

Answers


  1. Chosen as BEST ANSWER

    I found a way to do it. I was already testing but I was doing something wrong:

    csv_filename = csv_tst.csv
    
    with open(input_folder_json_files, 'r') as json_one_line_read:
         json_one_line_file = json.load(json_one_line_read)     
            
    with open(output_folder + csv_filename, 'a+', newline='') as csv_file:
         writer = csv.writer(csv_file)
         writer.writerow([json_one_line_file])
    

    This part of the code is within the FOR-loop that creates flattened JSON files. Perhaps there are more elegant ways to do it, but it works.


  2. You can install and import pandas, then use a ‘.to_csv()’.

    For ex.:

    import pandas as pd
    df = pd.read_json (r'Path where the JSON file is savedFile Name.json')
    df.to_csv (r'Path where the new CSV file will be storedNew File Name.csv', index = None)
    

    You can find the documentation here -> pandas.pydata.org

    Login or Signup to reply.
  3. The solution is to add all your data into a dictionary or other data structure like a list.

    After that use the csv library to write a proper CSV file.

    https://docs.python.org/3/library/csv.html

    Tha will look like this.

    I would use ; considering you have a list of names separated by ,.

    But why not write it all in a JSON file instead of a CSV.

    A CSV will look like this.

    
    name;age;married;divorced;children;pets;cars
    John;30;true;false;Ann,Billy;;model,BMW 230,mpg,27.5,model,Ford Edge,mpg,24.1
    
    ....
    

    It is preferred to store it all in a JSON, so you can keep the data schema.

    Anyways you need to store your data in a dict or a list and then write it to a JSON file with json library

    https://docs.python.org/3/library/json.html

    The json file will look like you said.

    {"name": "John", "age": 30, "married": true, "divorced": false, "children": ["Ann", "Billy"], "pets": null, "cars": [{"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1}]}
    {"name": "Doe", "age": 33, "married": true, "divorced": false, "children": ["Peter", "Billy"], "pets": null, "cars": [{"model": "Tesla", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 27.1}]}
    {"name": "Kurt", "age": 13, "married": true, "divorced": false, "children": ["Bruce", "Nikola"], "pets": null, "cars": [{"model": "Mercedes", "mpg": 27.5}, {"model": "123", "mpg": 24.1}]}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search