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

Greetings fellow enthusiasts! I am working on a project and I am utilizing python to interact with the twitter api.

Goal: Extract the location, text of tweet, created at, and user id from the raw data you see returned in the code from the "pprint.pprint(datares)" into a specified format in csv file.

Problem: How can I get the info I am returning into a csv file so that every row in the file would display as follows:

Row: tweet text, it's location, created at, user id

The following is my code and shows what I am able to return so far.

import urllib2, json, pprint, codecs, unicodedata

u = urllib2.urlopen('http://search.twitter.com/search.json?geocode=29.762778,-95.383056,25.0mi&page=1&rpp=20')
datares = json.load(u)
##raw data returned
pprint.pprint(datares)

##open csv file
with codecs.open('Geotweets.csv',mode='w', encoding='utf-8',errors='replace') as cache:
##need to save tweets,date,area,id to file
    for tweet in datares['results']:
        print tweet['text']
        archive=tweet['text']
        unicodedata.normalize('NFKD', archive).encode('ascii','ignore')
        cache.write(archive)


for date in datares['results']:
    print date['created_at']
for area in datares['results']:
    print area['location']
for id in datares['results']:
    print id['from_user']
share|improve this question

1 Answer

up vote 1 down vote accepted

If you want to make a CSV file, use the csv module!

E.g:

with codecs.open('Geotweets.csv',mode='w', encoding='utf-8',errors='replace') as cache:
    writer = csv.writer(cache)
    for tweet in datares['results']:
        writer.writerow([tweet['text'], tweet['area'], tweet['date'], tweet['id']])

Or alternatively:

with codecs.open('Geotweets.csv',mode='w', encoding='utf-8',errors='replace') as cache:
    writer = csv.DictWriter(cache, ["text", "area", "date", "id"])
    for tweet in datares['results']:
        writer.writerow(tweet)

Obviously, you could also use writerows() to simplify this further:

with codecs.open('Geotweets.csv',mode='w', encoding='utf-8',errors='replace') as cache:
    writer = csv.DictWriter(cache, ["text", "area", "date", "id"])
    writer.writerows(datares['results'])
share|improve this answer
Your insight is GREATLY appreciated @Lattyware and your first solution you listed proved to be very useful and beneficial thank you! – Techsan May 4 '12 at 0:17

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.