【问题标题】:inconsistent use of tabs and spaces in indentation notepad++ Python缩进记事本++ Python中制表符和空格的不一致使用
【发布时间】:2018-11-14 23:57:22
【问题描述】:

仅添加一行代码后,我在缩进错误中得到一个不一致的制表符和空格使用,我不明白为什么会抛出错误,这里是代码:

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:...

标签: python indentation


【解决方案1】:

这正是听起来的意思:您的代码在某些地方用空格缩进,而在其他地方用制表符缩进。要解决此问题,请在 Notepad++ 中转到 Edit -> Blank Operations -> TAB to Space(PEP 8 建议使用空格与制表符) .

【讨论】:

    【解决方案2】:

    要确定 Python 脚本中的间距,您可以使用一些复杂的文本编辑器。我建议您使用 Sublime Text 3 或 Atom,以便您下次轻松处理。

    【讨论】:

    • 谢谢你的建议,我会调查那些编辑
    • 我一直更喜欢 Visual Studio(个人/教育/开源使用的免费 IDE)或 Visual Studio Code(所有用途的免费文本编辑器)。 VS Code 建立在与 Atom 相同的框架之上,在许多方面都相似,但速度更快。而且 Sublime 需要花钱……无论如何,这是基于意见的,但我想我会投入两分钱。
    猜你喜欢
    • 2012-10-10
    • 2011-08-06
    • 2015-08-28
    • 2019-04-15
    • 2020-01-23
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多