Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is the simple code I'm using:

import tweepy

result = tweepy.api.search(q="McDonalds ",rpp=1000,page=2, geocode= "34.085422,-117.900879,100mi" )

for tweet in result:
    print tweet.text
    print tweet.geo

It returns the results I want. In my IDE I get results like this:

@stevo_k14 I'm going back after mcdonalds haha

None

“@FunnyEvil: If bars can't serve drunk people, Mcdonalds shouldn't be able to serve fat people.”

{u'type': u'Point', u'coordinates': [33.9581, -118.1779]}

Mcdonalds for breakfast? Yup i think so :)

None

@JessicaCarrillo You make me want to go out to McDonalds right now!!!!! Lol good morning.

{u'type': u'Point', u'coordinates': [33.9443, -118.0038]}

Some return no location and some return location. What I'd really like to do is write the results straight to a csv file with Keyword, Tweet, Lat, Lon as my headers. Keyword would be "McDonalds" in this case. It would also be nice to have some ifelse statement to not write results unless they have location.

I am new to programming so your help is much appreciated! Thanks

share|improve this question

1 Answer

up vote 0 down vote accepted

You can just use the csv module and iterate over your existing tweet.

import tweepy
import csv

keyword = 'McDonalds '
result = tweepy.api.search(q=keyword,rpp=1000,page=2, geocode= "34.085422,-117.900879,100mi" )

with open('some.csv', 'w') as acsv:
    w = csv.writer(acsv)
    w.writerow(('Keyword', 'Tweet', 'Lat', 'Lon'))
    for tweet in result:
        lat, lon = tweet.geo if tweet.geo else ('', '')
        w.writerow((keyword, tweet.text, lat, lon))
share|improve this answer
Do I need to create a blank csv and then put the path name in 'some.csv'? – niemoy Jun 1 '12 at 18:06
I replaced 'some.csv' with 'C:\temp\results.csv'. the 'results.csv is blank. The error i get is: Traceback (most recent call last): File "C:\Users\Niemann\Desktop\simplesearch.py", line 7, in <module> with open('C:\temp\results.csv', 'r') as acsv: IOError: [Errno 22] invalid mode ('r') or filename: 'C:\temp\results.csv' – niemoy Jun 1 '12 at 18:36
Sorry, should've been w, not r. You're writing the CSV, not reading it. By the way, read about reading and writing files and the csv module – kojiro Jun 1 '12 at 19:39
Yes I tried that before my last post and should have included that. I still get the same error with a 'w' instead of 'r' – niemoy Jun 1 '12 at 22:56
I changed code to this and made it to the last line keyword = 'McDonalds ' result = tweepy.api.search(q=keyword,rpp=1000,page=2, geocode= "34.085422,-117.900879,100mi" ) csvfile = r'C:\temp\results.csv' with open(csvfile, 'w') as acsv: w = csv.writer(acsv) w.writerow(('Keyword', 'Tweet', 'Lat', 'Lon')) for tweet in result: lat, lon = tweet.geo if tweet.geo else ('', '') w.writerow((keyword, tweet.text, lat, lon)) – niemoy Jun 1 '12 at 23:02
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.