Python Forum

Full Version: Write json data to csv
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I am stuck with my python code. What i'm trying to do is write some informations in a json format, to a csv file.
It's dealing with GPS position over a day.
The json data returned from the API call is something like :
[
{"date": "2024-10-20T20:56:28+00:00", "latitude": 43.3013946, "longitude": 1.6932394},
{"date": "2024-10-20T20:54:24+00:00", "latitude": 43.3014407, "longitude": 1.6933062},
{"date": "2024-10-20T20:53:23+00:00", "latitude": 43.3014778, "longitude": 1.6933793}
]
and so on.

My code looks like :

import csv

    position = [{"date": "2024-10-20T20:56:28+00:00", "latitude": 43.3013946, "longitude": 1.6932394}, {"date": "2024-10-20T20:54:24+00:00", "latitude": 43.3014407, "longitude": 1.6933062}, {"date": "2024-10-20T20:53:23+00:00", "latitude": 43.3014778, "longitude": 1.6933793}]
    date = position[0]["date"]
    lat = position[0]["latitude"]
    lon = position[0]["longitude"]

    data = [date, lat, lon]

    with open('C:/positions.csv', 'a', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(data)
But only one row is written to the csv file, desired all rows ...
I've tried with the writerows, but with no luck. Read also some python docs, but as a newbie in programming, it's a headache :)

Thanks for any help !
position[0] writes only the first entry

import csv
  
position = [{"date": "2024-10-20T20:56:28+00:00", "latitude": 43.3013946, "longitude": 1.6932394}, {"date": "2024-10-20T20:54:24+00:00", "latitude": 43.3014407, "longitude": 1.6933062}, {"date": "2024-10-20T20:53:23+00:00", "latitude": 43.3014778, "longitude": 1.6933793}]
with open('positions.csv', 'a', newline='') as f:
    writer = csv.writer(f)
    for x in range(len(position)):
        date = position[x]["date"]
        lat = position[x]["latitude"]
        lon = position[x]["longitude"]
      
        data = [date, lat, lon]
        print(data)
 
        writer.writerow(data)
csv

Output:
2024-10-20T20:56:28+00:00,43.3013946,1.6932394 2024-10-20T20:54:24+00:00,43.3014407,1.6933062 2024-10-20T20:53:23+00:00,43.3014778,1.6933793
Can also use Pandas for this.
import pandas as pd

position = [
    {"date": "2024-10-20T20:56:28+00:00", "latitude": 43.3013946, "longitude": 1.6932394},
    {"date": "2024-10-20T20:54:24+00:00", "latitude": 43.3014407, "longitude": 1.6933062},
    {"date": "2024-10-20T20:53:23+00:00", "latitude": 43.3014778, "longitude": 1.6933793}
]

df = pd.DataFrame(position)
df['date'] = pd.to_datetime(df['date'])
# To .csv
df.to_csv('gps_positions.csv', index=False)
Output:
G:\div_code\foobar λ python -i gps.py >>> df date latitude longitude 0 2024-10-20 20:56:28+00:00 43.301395 1.693239 1 2024-10-20 20:54:24+00:00 43.301441 1.693306 2 2024-10-20 20:53:23+00:00 43.301478 1.693379 >>> exit() G:\div_code\foobar λ cat gps_positions.csv date,latitude,longitude 2024-10-20 20:56:28+00:00,43.3013946,1.6932394 2024-10-20 20:54:24+00:00,43.3014407,1.6933062 2024-10-20 20:53:23+00:00,43.3014778,1.6933793
In csv you can use a DictWriter. It works well for writing a list of dictionaries.
import csv

position = [
    {"date": "2024-10-20T20:56:28+00:00", "latitude": 43.3013946, "longitude": 1.6932394},
    {"date": "2024-10-20T20:54:24+00:00", "latitude": 43.3014407, "longitude": 1.6933062},
    {"date": "2024-10-20T20:53:23+00:00", "latitude": 43.3014778, "longitude": 1.6933793},
]

with open("positions.csv", "w", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=position[0])
    writer.writeheader()
    writer.writerows(position)
positions.csv
Output:
date,latitude,longitude 2024-10-20T20:56:28+00:00,43.3013946,1.6932394 2024-10-20T20:54:24+00:00,43.3014407,1.6933062 2024-10-20T20:53:23+00:00,43.3014778,1.6933793
Perfect, this help me so much ! Thanks to both of you.
If i can ask something more, now that i have the csv with all the informations, i used the datetime lib to name the csv :

file = datetime.now().strftime('C:/positions-%d-%m-%Y-%H-%M.csv')
But all the datas are from the day before, so i'd like the file to be named C:/positions-%d(minus 1)-%m-%Y-%H-%M.csv.
Do you think there is a way to do this ?
Olive Wrote:But all the datas are from the day before, so i'd like the file to be named C:/positions-%d(minus 1)-%m-%Y-%H-%M.csv.
from datetime import datetime, timedelta

# Subtract one day from the current date
yesterday = datetime.now() - timedelta(days=1)
file = yesterday.strftime('C:/positions-%d-%m-%Y-%H-%M.csv')
print(file)
Once again, perfect Cool
Big thanks !