Reputation: 196
So i am trying to import json data from file and want to export in CSV file. Only few tags like "authors" and "title" work fine with this code but when i try that for "abstract" it split every word of abstract in new column of csv. Before I try split() it was doing the same for every character
here is my code
import json
import csv
filename="abc.json"
csv_file= open('my.csv', 'w',encoding="utf-8")
csvwriter = csv.writer(csv_file)
with open(filename, 'r') as f:
for line in f:
data = json.loads(line)
if 'abstract' in data:
csvwriter.writerow(data['abstract'].split())
elif 'authors' in data:
csvwriter.writerow(data['authors'])
else:
f="my"
sample json file can be downloaded from here http://s000.tinyupload.com/?file_id=28925213311182593120
Upvotes: 0
Views: 62
Reputation: 1784
The reason this happened in abstract
is because the value of abstract
is a string (in contrast, the value of authors
is a list). writerow
receives an iterable, and when you iterate over a string in python you get a letter each time.
So before you used split, python took the string and divided it into letters, thereby giving you one letter per column. When you used split, you transformed the string into a list of words, so when you iterate over it you get a word each time.
If you want to split abstract
by periods, just do the same thing with .split('.')
Upvotes: 1
Reputation: 21
Like Ben said, it would be great to see a sample from the JSON file, but the issue could be with how your trying to split your abstract data. With what you're doing now, you're asking it to split at every space. Try something like this if you're wanting to split by line:
if 'abstract' in data:
csvwriter.writerow(data['abstract'].split(","))
Upvotes: 1