PaP*_*nG0 4 python indentation
仅添加一行代码后,我在缩进错误中使用的制表符和空格不一致,并且我看不到为什么会引发错误,这是代码:
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)
Run Code Online (Sandbox Code Playgroud)
错误出现的地方是#lang = json_tweet_data ['lang']行,如果像显示的代码中所示将其删除或注释掉,它将正常工作,从我看到的所有内容都可以看到缩进的情况,可能是这里的问题吗?
这意味着听起来像是什么:在某些地方用空格缩进代码,而在另一些地方用制表符缩进代码。要解决此问题,请在Notepad ++中,转到“ 编辑” ->“ 空白操作” ->“将标签设置为空格”(PEP 8建议使用空格与制表符)。