Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inconsistent use of tabs and spaces in indentation notepad++ Python

I am getting an inconsistent use of tabs and spaces in indentation error after adding only one line of code and I can't see why it would throw the error, here is the code:

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)

the #lang = json_tweet_data['lang'] line is where the error is appearing and if I remove it or comment it out like it is in the code shown, it will work fine, from what I can see everything is indented fine, what could be the issue here?

like image 911
Charlie Ansell Avatar asked Nov 14 '18 23:11

Charlie Ansell


People also ask

How do you fix inconsistent tabs and spaces in indentation in Python?

The Python "TabError: inconsistent use of tabs and spaces in indentation" occurs when we mix tabs and spaces in the same code block. To solve the error, remove the spacing and only use tabs or spaces, but don't mix the two in the same code block.

How do you fix inconsistent use of tabs and spaces in indentation VS code?

You can fix the tab inconsistency by converting all indentation to tab or spaces. If you open the "Show All Commands" tab, ( by pressing Ctrl+Shift+P or F1 ) and search for "convert indentation", two options will by available: convert indentation to tabs. convert indentation to spaces.

Are tabs or spaces the preferred method for indentation Python?

Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python disallows mixing tabs and spaces for indentation.

What is “taberror inconsistent use of tabs in indentation” in Python?

But if you mix the spaces and tabs in a program, Python gets confused. It then throws an error called “ TabError inconsistent use of tabs and spaces in indentation ”.

How do I fix inconsistent use of tabs and spaces in Python?

The Python “TabError: inconsistent use of tabs and spaces in indentation” error is raised when you try to indent code using both spaces and tabs. You fix this error by sticking to either spaces or tabs in a program and replacing any tabs or spaces that do not use your preferred method of indentation.

Should I use tabs or spaces for indentation in Python code?

When you use both tabs and spaces for indentation, Python interpreter raises an Exception, which is TabError. So don’t mix up tabs and spaces while indenting Python code. Either just use spaces, or use just spaces. But I recommend to use tabs, as they are much easier to use. By the way, I don’t think there is an option to do that in Notepad.

Why is my script indenting with 4 spaces?

it is because of using tabs and spaces for indenting code. in this script I replace each tab with four spaces. if you use sublime or any other editor which gives you the tool to replace text you can replace all tabs by four spaces from editor. Show activity on this post.


1 Answers

It means exactly what is sounds like: you indented your code with spaces in some places and with tabs in others. To fix this, in Notepad++, go to Edit -> Blank Operations -> TAB to Space (PEP 8 recommends using spaces vs. tabs).

like image 157
iz_ Avatar answered Nov 14 '22 21:11

iz_