我添加了一行代码后出现了“缩进中制表符和空格的使用不一致”错误,但我看不出为什么会出现这个错误,以下是代码:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) #create authentcation handler
auth.set_access_token(access_token, access_secret) #set access tokens to connect to twitter dev account
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) #consume tweepy api function,
tweets = api.user_timeline('realDonaldTrump', count=30)
tweets_list = []
for each_tweet in tweets:
tweets_list.append(each_tweet._json)
with open('tweets.csv', 'a') as file:
file.write(json.dumps(tweets_list, indent=4))
my_demo_list = []
with open('tweets.csv', encoding='utf-8') as csvFile:
all_data = json.load(csvFile)
for json_tweet_data in all_data:
tweet_id = json_tweet_data['id']
text = json_tweet_data['text']
favorite_count = json_tweet_data['favorite_count']
retweet_count = json_tweet_data['retweet_count']
created_at = json_tweet_data['created_at']
#lang= json_tweet_data['lang']
my_demo_list.append({'tweet_id': str(tweet_id),
'text': str(text),
'favorite_count': int(favorite_count),
'retweet_count': int(retweet_count),
'created_at': created_at,
'lang':str(lang)
})
print(my_demo_list)
tweet_json = pd.DataFrame(my_demo_list, columns =
['tweet_id', 'text',
'favorite_count', 'retweet_count',
'created_at','language'])
print(tweet_json)
#lang = json_tweet_data ['lang'] 这一行出现了错误,如果我将它移除或像代码中所示注释掉,那么它就可以正常工作。我认为每个缩进都很好,这里可能是什么问题?
with open('tweets.csv', 'a') as file:
... - l'L'l