如何使用Python 3通过15,000条推文将JSON文件转换为CSV

问题描述 投票:0回答:1

我已经使用tweepy流光API收集了15,000条阿拉伯推文用于研究。这些推文被保​​存在37个JSON文件中,最后被复制到一个JSON文件中。我正在尝试使用以下脚本将其转换为CSV:https://github.com/HMukdadi/json-csv-converter但我不断“错误加载文件...退出:额外数据:第3行第1列(字符7478)”另外,我是一名语言学家,并且我没有扎实的编程背景:)。

https://i.stack.imgur.com/k270b.png

python json csv twitter tweepy
1个回答
0
投票

这是正常的,因为json文件包含许多应该放在JSON数组或父json对象中的json对象,如果您不愿意,则发布2或3行,我将为您编写一个接受您的输入类型的脚本,或者在末尾添加:add [在开头],并在每个}执行最后一次后,该脚本将适用于您

此脚本将类似于示例的json文件转换为将与git repo中的脚本一起工作的有效json数组(已测试)

import sys

print("[")
with open(sys.argv[1]) as files:
    lines=files.readlines()
    for line in lines:
        if(line!="\n"):

            print(line)
        else:
            print(",")
    files.close()
print("]")

执行:python3 file.py [yourJsonFile] >> newJsonFile.json

然后使用newJsonFile.json执行json-csv-converter.py

enter image description here

假设您有SAMPLE.json文件:enter image description here

只需执行脚本:

enter image description here

然后执行json-csv-converter.py:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.