我正在尝试有选择地将JSON文件转换为CSV。我想重复JSON的意思。在
编写CSV的代码如下所示:
f = csv.writer(open("test.csv", "wb+"))
f.writerow(["id", "text", "polarity"])
for x in final:
f.writerow([x["id"],
x["text"],
x["polarity"]])
遗憾的是,我得到了以下错误:
^{pr2}$我已经知道问题出在哪里了。加载后我检查了JSON的类型。这是个口述,所以应该没问题。在
当我打印我的口述时:
print (final)
我得到:
{u'data': [{u'polarity': 2, u'text': u'How deep is your love - Micheal Buble Ft Kelly Rowland ?', u'meta': {u'language': u'en'}, u'id': u'1'}, {u'polarity': 2, u'text': u'RT @TrueTeenQuotes: #SongsThatNeverGetOld Nelly ft. Kelly Rowland - Dilemma', u'meta': {u'language': u'en'}, u'id': u'2'}, {u'polarity': 2, u'text': u'RT @GOforCARL: Dilemma - Nelly Feat. Kelly Rowland #Ohh #SongsThatNeverGetOld', u'meta': {u'language': u'en'}, u'id': u'3'}, {u'polarity': 2, u'text': u'#NP Kelly Rowland Grown Woman', u'meta': {u'language': u'en'}, u'id': u'4'}, {u'polarity': 2, u'text': u"My friend just said 'kelly rowland is gettin it good... Most of her songs are sexual'", u'meta': {u'language': u'en'}, u'id': u'5'}, {u'polarity': 2, u'text': u'No. Kelly Rowland is the Black Barbie, idc', u'meta': {u'language': u'en'}, u'id': u'6'}, {u'polarity': 2, u'text': u'Nelly - Gone ft. Kelly Rowland http://t.co/tXjhCS05l0', u'meta': {u'language': u'en'}, u'id': u'7'}, {u'polarity': 2, u'text': u'Kisses Down Low by Kelly Rowland killer?\u2018?\u2018?\u2018 #NellyVille', u'meta': {u'language': u'en'}, u'id': u'8'}]}
其中除了“polarity”的值外,每个项似乎都是Unicode格式。我现在有3个问题。1是否所有项目都是unicode格式?如何更改dict中的格式?这能解决我的问题吗?在
在Python中迭代字典会给您dict的键作为字符串-因此上面的示例中的
x
将只包含字符串"data"
如果要迭代与
final
dict的u"data"
键相关联的列表值中的dict,则必须用Python编写:相关问题 更多 >
编程相关推荐