I am currently trying to print data from a text file using pandas,JSON and Python 3.4.
When I run the code on Python 2.7 on my friend's machine it works fine, but not with Python 3.4.
Here is my code:
import json
import pandas as pd
tweets_data_path = 'tweets.txt'
tweets_data = []
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
try:
tweet = json.loads(line)
tweets_data.append(tweet)
except:
continue
print (len(tweets_data))
tweets = pd.DataFrame()
tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)
tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data)
tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data)
for i in range(len(tweets_data)):
print(tweets['text'][i])
Instead of printing the tweet data, it prints the memory location of the data. e.g.
<map object at 0x04988050>
<map object at 0x04988050>
How do I get it to print out the actual tweet data?
You need to convert it to a list first. So just change map(lambda...)
to list(map(lambda...))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With