我目前正在尝试使用pandas,JSON和Python 3.4打印文本文件中的数据.
当我在朋友的机器上运行Python 2.7上的代码时,它运行正常,但不适用于Python 3.4.
这是我的代码:
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])
它不打印推文数据,而是打印数据的内存位置.例如
<map object at 0x04988050>
<map object at 0x04988050>
如何打印出实际的推文数据呢?
解决方法:
您需要先将其转换为列表.所以只需将map(lambda …)更改为list(map(lambda …))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。