Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write json data to csv
#1
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 !
Reply
#2
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
Reply
#3
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
Reply
#4
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
Reply
#5
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 ?
Reply
#6
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)
Reply
#7
Once again, perfect Cool
Big thanks !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 2,309 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  write json into a table herobpv 4 2,671 Jan-22-2023, 04:36 AM
Last Post: herobpv
  Read nested data from JSON - Getting an error marlonbown 5 2,683 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 2,524 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Write sql data or CSV Data into parquet file mg24 2 4,204 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  Read JSON via API and write to SQL database TecInfo 5 4,946 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  Is this possible to write a script for checking data from website? WanW 2 1,808 Jun-02-2022, 02:31 AM
Last Post: Larz60+
  Convert nested sample json api data into csv in python shantanu97 3 5,456 May-21-2022, 01:30 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 3,323 May-09-2022, 02:49 AM
Last Post: snippsat
  json api data parsing elvis 0 1,377 Apr-21-2022, 11:59 PM
Last Post: elvis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020