how to convert json to csv in python. want it open in excel with the lat and long columns.
[
{
"Lat": "-122.37391463199998",
"Long": "47.630880207000075"
},
{
"Lat": "-122.38447021399998",
"Long": "47.70118823100006"
},
{
"Lat": "-122.34729431799997",
"Long": "47.64717111900006"
}
]
The csv module has a handy writerows() method on the DictWriter class:
import csv
import json
data = json.loads(""" My Json """)
with open('lat_long.csv', 'w') as csvfile:
fieldnames = ['Lat', 'Long']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
You can use the json module to convert your string into a dictionary. Then use the csv module to save them into a file.
import json, csv
json_string = '[{"Lat": "-122.37391463199998", "Long": "47.630880207000075"}, {"Lat": "-122.38447021399998", "Long": "47.70118823100006"}, {"Lat": "-122.34729431799997", "Long": "47.64717111900006"} ]'
data = json.loads(json_string)
with open('foo.csv', 'wb') as csvfile:
csv_writer = csv.DictWriter(csvfile, delimiter=',', fieldnames=['Lat', 'Long'])
csv_writer.writeheader()
csv_writer.writerows(data)
Related
I'm trying to convert csv format to JSON, I googled I'm not getting the correct way to modify it to get the desired one.
This is my code in python:
import csv
import json
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
#reading csv (encoding is important)
with open(csvFilePath, encoding='utf-8') as csvf:
#csv library function
csvReader = csv.DictReader(csvf)
#convert each csv row into python dictionary
for column in csvReader:
#add this python dictionary to json array
jsonArray.append(column)
#convertion
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4)
jsonf.write(jsonString)
csvFilePath='example.csv'
jsonFilePath='output.json'
csv_to_json(csvFilePath, jsonFilePath)
and this is my csv file format:
My actual JSON Output:
[
{
"Area": "IT",
"Employee": "Carl",
},
{
"Area": "IT",
"Employee": "Walter",
},
{
"Area": "Financial Resources",
"Employee": "Jennifer",
}
]
My desired JSON Output:
[
{
"Area": "IT",
"Employee": ["Carl","Walter"],
},
{
"Area": "Financial Resources",
"Employee": ["Jennifer"],
}
]
Thank you in advance!
Something like this should work.
def csv_to_json(csvFilePath, jsonFilePath):
areas = {}
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for column in csvReader:
area, employee = column["Area"], column["Employee"] # split values
if area in areas: # add all keys and values to one dictionary
areas[area].append(employee)
else:
areas[area] = [employee]
# convert dictionary to desired output format.
jsonArray = [{"Area": k, "Employee": v} for k,v in areas.items()]
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4)
jsonf.write(jsonString)
I am trying to covert my CSV email list to a JSON format to mass email via API. This is my code thus far but am having trouble with the output. Nothing is outputting on my VS code editor.
import csv
import json
def make_json(csvFilePath, jsonFilePath):
data = {}
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for rows in csvReader:
key = rows['No']
data[key] = rows
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
csvFilePath = r'/data/csv-leads.csv'
jsonFilePath = r'Names.json'
make_json(csvFilePath, jsonFilePath)
Here is my desired JSON format
{
"EmailAddress": "hello#youngstowncoffeeseattle.com",
"Name": "Youngstown Coffee",
"ConsentToTrack": "Yes"
},
Heres my CSV list
No,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen & Bakery,catering#zylberschtein.com,Yes
Youngstown Coffee,hello#youngstowncoffeeseattle.com,Yes
It looks like you could use a csv.DictReader to make this easier.
If I have data.csv that looks like this:
Name,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen,catering#zylberschtein.com,yes
Youngstown Coffee,hello#youngstowncoffeeseattle.com,yes
I can convert it into JSON like this:
>>> import csv
>>> import json
>>> fd = open('data.csv')
>>> reader = csv.DictReader(fd)
>>> print(json.dumps(list(reader), indent=2))
[
{
"Name": "Zylberschtein's Delicatessen",
"EmailAddress": "catering#zylberschtein.com",
"ConsentToTrack": "yes"
},
{
"Name": "Youngstown Coffee",
"EmailAddress": "hello#youngstowncoffeeseattle.com",
"ConsentToTrack": "yes"
}
]
Here I've assumed the headers in the CSV can be used verbatim. I'll update this with an exmaple if you need to modify key names (e.g. convert "No" to "Name"),.
If you need to rename a column, it might look more like this:
import csv
import json
with open('data.csv') as fd:
reader = csv.DictReader(fd)
data = []
for row in reader:
row['Name'] = row.pop('No')
data.append(row)
print(json.dumps(data, indent=2))
Given this input:
No,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen,catering#zylberschtein.com,yes
Youngstown Coffee,hello#youngstowncoffeeseattle.com,yes
This will output:
[
{
"EmailAddress": "catering#zylberschtein.com",
"ConsentToTrack": "yes",
"Name": "Zylberschtein's Delicatessen"
},
{
"EmailAddress": "hello#youngstowncoffeeseattle.com",
"ConsentToTrack": "yes",
"Name": "Youngstown Coffee"
}
]
and to print on my editor is it simply print(json.dumps(list(reader), indent=2))?
I'm not really familiar with your editor; print is how you generate console output in Python.
I am needing a Python script to convert CSV data to GeoJSON output. The output should match the format, below:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -85.362709,40.466442 ]
},
"properties": {
"weather":"Overcast",
"temp":"30.2 F"
}
}
]
}
I am using this script to run the process, but it does not produce the desired output:
import csv, json
li = []
with open('CurrentObs.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for latitude, longitude, weather, temp in reader:
li.append({
"latitude": latitude,
"longitude": longitude,
"weather": weather,
"temp": temp,
"geo": {
"__type": "GeoPoint",
"latitude": latitude,
"longitude": longitude,
}
})
with open("GeoObs.json", "w") as f:
json.dump(li, f)
Any help is greatly appreciated!
one could use directly the geojson package:
import csv, json
from geojson import Feature, FeatureCollection, Point
features = []
with open('CurrentObs.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for latitude, longitude, weather, temp in reader:
latitude, longitude = map(float, (latitude, longitude))
features.append(
Feature(
geometry = Point((longitude, latitude)),
properties = {
'weather': weather,
'temp': temp
}
)
)
collection = FeatureCollection(features)
with open("GeoObs.json", "w") as f:
f.write('%s' % collection)
Check if this script resolves
import csv
import json
from collections import OrderedDict
li = []
with open('CurrentObs.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for latitude, longitude, weather, temp in reader:
d = OrderedDict()
d['type'] = 'Feature'
d['geometry'] = {
'type': 'Point',
'coordinates': [float(latitude), float(longitude)]
}
d['properties'] = {
'weather': weather,
'temp': temp
}
li.append(d)
d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
with open('GeoObs.json', 'w') as f:
f.write(json.dumps(d, sort_keys=False, indent=4))
I have a CSV File with following contents:
source: data.opennepal.net
District,Zone,Geographical Region,Development Region,Causalities,In Number
Sindhupalchok,Bagmati,Mountain,Central,Total No. of Houses,66688
Sindhupalchok,Bagmati,Mountain,Central,Total Population,287798
Sindhupalchok,Bagmati,Mountain,Central,Dead Male,1497
Sindhupalchok,Bagmati,Mountain,Central,Dead Female,1943
Kathmandu,Bagmati,Hill,Central,Total No. of Houses,436344
Kathmandu,Bagmati,Hill,Central,Total Population,1744240
Kathmandu,Bagmati,Hill,Central,Dead Male,621
Kathmandu,Bagmati,Hill,Central,Dead Female,600
My objective is to generate a JSON object like this from it:
{
"district":{
"Sindhupalchok":{
"Causalities":{
"Total No. of Houses":66688,
"Total Population":287798,
"Dead Male":1497,
"Dead Female":1943
},
"geoInfo":{
"Zone":"Bagmati",
"geography":"Mountain",
"Dev Region":"Central"
}
},
"Kathmandu":{
"Causalities":{
"Total No. of Houses":436344,
"Total Population":1744240,
"Dead Male":621,
"Dead Female":600
},
"geoInfo":{
"Zone":"Bagmati",
"geography":"Hill",
"Dev Region":"Central"
}
}
}
}
I've tried using csv.DictReader(csvfile, fieldnames) but it generates redundant nodes in JSON which is difficult to parse and unnecessarily lenghty.
I am using python 2.x
This is my attempt so far:
>>> csvData = open('data.csv','rb')
>>> fieldnames = ("district", "zone", "geographicalRegion", "developmentRegion", "causalities", "injuredNumber")
>>> reader = csv.DictReader(csvData, fieldnames)
>>> rawJson = json.dumps([ row for row in reader ])
rawJson isn't the one I've been seeking. It just maps the fieldnames with individual datasets.
So the question is: How can I create this JSON object without redundant nodes?
As glibdud mentions in the comments you need to loop over the data a bit more manually, so that you can create the desired JSON structure.
We read each line of the CSV data as a dict, and check if we've encountered a new district, and if so we create a new data dict for it, and insert a geoInfo dict into data. Then we can gather the casualty data from that line and the subsequent lines for that district. And once we've gathered all that data we can insert the data dict into the main all_data dict.
To test the code I put your .csv data into a file called 'qdata.csv'
import csv
import json
filename = 'qdata.csv'
fieldnames = ('district', 'Zone', 'geography',
'Dev Region', 'casualties', 'injured')
geo_keys = ('Zone', 'geography', 'Dev Region')
all_data = {}
with open(filename, 'rb') as csvfile:
reader = csv.DictReader(csvfile, fieldnames)
# skip header
next(reader)
current_district = None
for row in reader:
district = row['district']
if district != current_district:
current_district = district
data = all_data[district] = {}
casualties = data['Casualties'] = {}
data['geoInfo'] = dict((k, row[k]) for k in geo_keys)
casualties[row['casualties']] = row['injured']
print json.dumps(all_data, indent=4, sort_keys=True)
Output
{
"Kathmandu": {
"Casualties": {
"Dead Female": "600",
"Dead Male": "621",
"Total No. of Houses": "436344",
"Total Population": "1744240"
},
"geoInfo": {
"Dev Region": "Central",
"Zone": "Bagmati",
"geography": "Hill"
}
},
"Sindhupalchok": {
"Casualties": {
"Dead Female": "1943",
"Dead Male": "1497",
"Total No. of Houses": "66688",
"Total Population": "287798"
},
"geoInfo": {
"Dev Region": "Central",
"Zone": "Bagmati",
"geography": "Mountain"
}
}
}
This output isn't exactly what you've got in your question, but I think you should be able to take it from here. :)
I have a test.csv file:
foo,bar,foobar,barfoo
1,2,3,4
5,6,7,8
9,10,11,12
And the following CSV parser:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
import json
f = open ( 'test.csv', 'r' )
reader = csv.DictReader( f, fieldnames = ( "foo","bar","foobar","barfoo" ))
out = json.dumps( [ row for row in reader ], ensure_ascii=False, encoding="utf-8")
print out
Is there an easy way to replace the fieldnames in the output, without changing the header of the CSV file?
My current output is this:
[
{
"foobar":"foobar",
"foo":"foo",
"bar":"bar",
"barfoo":"barfoo"
},
{
"foobar":"3",
"foo":"1",
"bar":"2",
"barfoo":"4"
},
{
"foobar":"7",
"foo":"5",
"bar":"6",
"barfoo":"8"
},
{
"foobar":"11",
"foo":"9",
"bar":"10",
"barfoo":"12"
}
]
Could I get something like this:
[
{
"id":"foobar",
"email":"foo",
"name":"bar",
"phone":"barfoo"
},
{
"id":"3",
"email":"1",
"name":"2",
"phone":"4"
},
{
"id":"7",
"email":"5",
"name":"6",
"phone":"8"
},
{
"id":"11",
"email":"9",
"name":"10",
"phone":"12"
}
]
The easiest way is to just set:
reader.fieldnames = "email", "name", "id", "phone"
You can save the old fieldnames if you want too.
Just replace this line:
reader = csv.DictReader(f, fieldnames = ( "foo","bar","foobar","barfoo" ))
with this:
reader = csv.DictReader(f, fieldnames=("id", "email", "name", "phone"))