我试图从json转换成csv,但是列表中的每个单词前面都出现了一个额外的字母“u”,我用pandas来读取这个csv数据, 这是我的代码:
import json
import csv
with open("train.json") as file:
data = json.load(file)
with open("trainc.csv", "w") as file:
csv_file = csv.writer(file)
csv_file.writerow(data[0].keys())
for item in data:
csv_file.writerow(item.values())
import pandas as pd
train = pd.read_csv("trainc.csv", header=0)
作为json文件的示例,这是第一个:
^{pr2}$我用这条线打印配料
print train['ingredients'][0]
当我打印相同的记录输出时是这样的:
[u'romaine lettuce', u'black olives', u'grape tomatoes', u'garlic', u'pepper', u'purple onion', u'seasoning', u'garbanzo beans', u'feta cheese crumbles']
此
u
不在字符串中。它只是说数据的类型是unicode。在您可以看到数据中没有额外的
u
。在Python str vs unicode types
http://www.diveintopython.net/xml_processing/unicode.html
相关问题 更多 >
编程相关推荐