Feb-24-2017, 10:17 PM
Hello! I am new to the programming and learn everything in Python from scratch.
My goal is to (1) import tweets from a JSON file, (2) pull out the data of interest from the tweets, and (3) save the extracted data to a CSV. Building on the examples provided by other users online, I came up with the following code.
However, there seems to be a problem that falls out of my comprehension
And for that I am seeking your help. I include the error message below:
My goal is to (1) import tweets from a JSON file, (2) pull out the data of interest from the tweets, and (3) save the extracted data to a CSV. Building on the examples provided by other users online, I came up with the following code.
# import necessary modules
import json
from csv import writer
# import tweets from JSON
with open('00.json') as in_file, \
open('test.csv', 'w') as out_file:
print >> out_file, 'ids, text, lang, geo, place'
csv = writer(out_file)
tweet_count = 0
for line in in_file:
tweet_count += 1
tweet = json.loads(line)
# Pull out various data from the tweets
row = (
tweet['id_str'], # author_id
tweet['text'], # tweet_time
tweet['lang'], # tweet_language
tweet['geo'], # tweet_geo
tweet['place'], # tweet_place
)
values = [(value.encode('utf8') if hasattr(value, 'encode') else value) for value in row]
csv.writerow(values)
# print the name of the file and number of tweets imported
print ("File Imported:"), str('00.json')
print ("# Tweets Imported:"), tweet_count
print ("File Exported:"), str('test.csv') However, there seems to be a problem that falls out of my comprehension
And for that I am seeking your help. I include the error message below:Error:[color=#000000]TypeError Traceback (most recent call last)
[/color]
<ipython-input-56-2e4d7aed55d0> in <module>()
5 # import tweets from JSON
6 with open('00.json') as in_file, open('test.csv', 'w') as out_file:
----> 7 print >> out_file, 'ids, text, lang, geo, place'
8 csv = writer(out_file)
9 tweet_count = 0
[color=#000000]TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'[/color]Thank you in advance for your guidance!
