简体   繁体   English

我的python代码未将JSON数据正确导入CSV

[英]My python code is not importing JSON data correctly to CSV

So i am trying to import json data from file and want to export in CSV file. 所以我试图从文件中导入json数据,并想导出为CSV文件。 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. 只有很少的标签(如“作者”和“标题”)可以在此代码中正常工作,但是当我尝试将其用于“抽象”时,它将csv的新列中的抽象单词分割开来。 Before I try split() it was doing the same for every character 在尝试split()之前,它对每个字符都执行相同的操作

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 可以从此处http://s000.tinyupload.com/?file_id=28925213311182593120下载示例json文件

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. 就像Ben所说的那样,很高兴看到JSON文件中的示例,但是问题可能出在您如何尝试拆分抽象数据上。 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(","))

The reason this happened in abstract is because the value of abstract is a string (in contrast, the value of authors is a list). 这件事发生在原因abstract是因为价值abstract为一个字符串(相反,价值authors是一个列表)。 writerow receives an iterable, and when you iterate over a string in python you get a letter each time. writerow收到一个可迭代对象,当您在python中迭代一个字符串时,每次都会得到一个字母。

So before you used split, python took the string and divided it into letters, thereby giving you one letter per column. 因此,在使用split之前,python将字符串取为字符串并将其分为字母,从而每列给您一个字母。 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. 使用split时,您将字符串转换为单词列表,因此在对其进行迭代时,每次都会得到一个单词。

If you want to split abstract by periods, just do the same thing with .split('.') 如果.split('.')句点分割abstract ,请使用.split('.')做同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM