python - 缩进记事本++ Python中制表符和空格的不一致使用
问题描述
仅添加一行代码后,我在缩进错误中得到不一致的制表符和空格使用,我不明白为什么它会抛出错误,这里是代码:
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'] 行是出现错误的地方,如果我删除它或像显示的代码中那样将其注释掉,它将正常工作,据我所见,一切都很好,什么可能是这里的问题吗?
解决方案
这意味着听起来像:您在某些地方用空格缩进了代码,在其他地方用制表符缩进。要解决此问题,请在 Notepad++ 中转到编辑->空白操作->制表符到空格(PEP 8 建议使用空格与制表符)。
推荐阅读
- python - 无法在中间模型中将 null 保存到 ForeignKey - Django
- dask - 使用 Dask 从多个 Python 进程编写 Parquet 文件
- c - 内核模块中已定义但未使用的变量消息
- r - 在 RStudio 中显示或呈现 html 的一般方法(就像在浏览器中一样)?
- html - 为什么 display: flex 会导致元素上下颠倒?
- pdf-generation - 快照测试 PDF
- wpf - 触发器覆盖绑定
- mysql - Laravel SQL 查询。帮我
- python - 如何在谷歌 colab 中显示 catboost 图?
- java - 在数组中使用多线程