CSV到JSON文件转换

时间:2018-05-28 22:23:32

标签: python json csv

我将CSV文件转换为JSON格式时遇到一些困难: 我的CSV按以下顺序排列,TAB间距为'label','tweet'为列名,TAB间隔。

label                tweet
0                    I love you Sydney #Sydneycafeseige 
1                    Please do not use today as an excuse

但我喜欢在JSON文件中将数据转换为以下JSON格式

{"tweet": " I love you Sydney #Sydneycafeseige", "label": 0}
{"tweet": "Please do not use today as an excuse ", "label": 0}
{"tweet": "Outrage as bystanders", "label": 0}
{"tweet": "This man carries a gun ", "label": 0}

我尝试使用此脚本但无法正常工作:

import csv
import json

csvfile = open('D:\\datasets\\wazeed\\sts_gold_tweet.csv', 'r')
jsonfile = open('D:\\datasets\\wazeed\\file.json', 'w')

fieldnames = ("label","tweet")

reader = csv.DictReader( csvfile)

for row in reader:
  json.dump(row, jsonfile)
  jsonfile.write('\n')

1 个答案:

答案 0 :(得分:0)

有了这些问题,如果有疑问,您可以直接使用csv.reader,这可以在构建词典列表时提供更多控制。然后在一次通话中使用json.dump

这是一个演示:

from io import StringIO
import csv, json

mystr = StringIO("""label\ttweet
0\tI love you Sydney #Sydneycafeseige
1\tPlease do not use today as an excuse""")

data = []

# replace mystr with open('file.csv', 'r')
with mystr as f:
    reader = csv.reader(f, delimiter='\t')
    headers = next(reader)
    for line in reader:
        data.append({headers[0]: int(line[0]), headers[1]: line[1]})

with open('file.json', 'w') as fout:
    json.dump(data, fout)

with open('file.json', 'r') as fin:
    d = json.load(fin)

print(d)

[{'label': 0, 'tweet': 'I love you Sydney #Sydneycafeseige'},
 {'label': 1, 'tweet': 'Please do not use today as an excuse'}]