如何使用python3将一个JSON文件转换成包含15000条tweets的CSV

2024-05-14 10:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here我收集了15000条阿拉伯语tweets,用于使用tweepy streamer API进行研究。tweets被保存在37个JSON文件中,最后被复制到一个JSON文件中。我正在尝试使用以下脚本将其转换为CSV:https://github.com/HMukdadi/json-csv-converter 但我一直在
“加载文件时出错。。。退出:额外数据:第3行第1列(char 7478) “
另外,我是语言学家,没有扎实的编程背景:)。你知道吗

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


Tags: 文件csvhttpsimagegithub脚本comapi
1条回答
网友
1楼 · 发布于 2024-05-14 10:29:51

这是正常的,因为json文件包含许多json对象,这些对象应该在json数组或父json对象中。你知道吗

如果你愿意的话,我会给你写一个接受你输入类型的脚本,或者:在结尾加上[在开头],在每一行后面加上}除了最后一行,这个脚本就可以了

此脚本将类似于示例的json文件转换为有效的json数组,该数组将与git repo(tested)中的脚本一起使用

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

然后执行json csv-转换器.py与newJsonFile.json文件你知道吗

enter image description here

假设你有示例.json文件: enter image description here

只需执行脚本:

enter image description here

然后执行json csv-转换器.py地址:

enter image description here

相关问题 更多 >

    热门问题