如何将json文件保存为txt文件?

时间:2018-11-04 14:26:50

标签: python json python-3.x

我想将json文件保存为txt文件。我的txt文件应如下所示: enter image description here

这是我的代码:

# For loop which will add each available tweet to a new line of tweet_json.txt
with open('tweet_json.txt', 'a', encoding='utf8') as f:
    for tweet_id in twitter_archive['tweet_id']:
        try:
            tweet = api.get_status(tweet_id, tweet_mode='extended')
            json.dump(tweet._json, f)
            f.write('./')
        except:
            continue

    # For loop to append each tweet into a list
tweets_data = []

tweet_file = open('tweet_json.txt', "r")

for line in tweet_file:
    try:
        tweet = json.loads(line)
        tweets_data.append(tweet)
    except:
        continue

tweet_file.close()

使用此代码,我得到以下输出: enter image description here

感谢您的帮助!

0 个答案:

没有答案
相关问题