Python Twitter API Stream tweepy trying to save data to a CSV file
<pre><code>from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

ckey = 'hidden due to question'
csecret = 'hidden due to question'
atoken = 'hidden due to question'
asecret = 'hidden due to question'

class listener(StreamListener):

    def on_data(self, data):
        try:
            print (data)
            saveFile = open('TwitterAPI.csv','a')
            saveFile.write(data)
            saveFile.Write('\n')
            saveFIle.close()
            return (True)
    def on_error(self, status):
        print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])
</code></pre>

<p>The problem with the code above is the fact that it keeps on coming up with an error message stating that there is an unexpected unindent at the //def on_error// row</p>

<p>You open a <code>try</code> block without catching the exception.</p>

<p><a href="https://docs.python.org/3/tutorial/errors.html" rel="nofollow">https://docs.python.org/3/tutorial/errors.html</a></p>

<p>Also be careful, python is case sensitive, so <code>saveFile</code> is not <code>saveFIle</code>, nor <code>saveFile.write()</code> is <code>saveFile.Write()</code>...</p>

<p>Editing your <code>on_data()</code> handler as follow should make it work :</p>

<pre><code>def on_data(self, data):
    try:
        print(data)
        with open('TwitterAPI.csv','a') as f:
            f.write(data)
    except Exception as e: # here catch whatever exception you may have.
        print('[!] Error : %s' % e)
</code></pre>

<p><strong>Edit :</strong> Below is your full code reworked :</p>

<pre><code>from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

ckey = 'hidden due to question'
csecret = 'hidden due to question'
atoken = 'hidden due to question'
asecret = 'hidden due to question'


class listener(StreamListener):

    def on_data(self, data):
        try:
            print(data)
            with open('TwitterAPI.csv','a') as f:
                f.write(data)
        except:
            pass

    def on_error(self, status):
        print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])
</code></pre>

python api csv twitter