gpt4 book ai didi

python - 读取和存储 JSON 数据的有效方法

转载 作者:太空宇宙 更新时间:2023-11-03 21:27:50 25 4
gpt4 key购买 nike

tweets_data = []
print('Opening file')
tweets_file = open('twitter_data.txt') #Its a file which has twitter data in JSON
start_time = time.time()
print('List generation in process')
for line in tweets_file:
try:
tweet = json.loads(line)
tweets_data.append(tweet)
except:
continue
tweets_file.close()
print(len(tweets_data))

我正在使用上面的代码创建一个列表,作为 Twitter 数据(通过 twitter api 流获得约 3GB),但我的程序运行了超过 3 小时。我需要一种有效的方法来做到这一点,因为我想使用这个列表来构建数据框。

最佳答案

我不确定

tweets_data = []
print('Opening file')
tweets_file = open('twitter_data.txt') #Its a file which has twitter data in JSON
start_time = time.time()
append = tweets_data.append
print('List generation in process')
for line in tweets_file:
try:
tweet = json.loads(line)
append(tweet)
except:
continue
tweets_file.close()
print(len(tweets_data))

关于python - 读取和存储 JSON 数据的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53738873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com