将Python脚本输出保存为JSON文件
我正在运行一个Python脚本,我想提取其中的一部分并把它转换成JSON文件,这样我就可以把它传递给Google可视化工具。
我已经附上了我的Python脚本、输出结果和详细信息。
我在做什么:
我正在尝试从AlchemyAPI运行这个Python脚本。
https://github.com/AlchemyAPI/alchemyapi-twitter-python
我的输出结果如下:我从输出中得到了统计数据。
##########################################################
# The Tweets #
##########################################################
@uDiZnoGouD
Date: Mon Apr 07 05:07:19 +0000 2014
To enjoy in case you win!
To help you sulk in case you loose!
#IndiavsSriLanka #T20final http://t.co/hRAsIa19zD
Document Sentiment: positive (Score: 0.261738)
##########################################################
# The Stats #
##########################################################
Document-Level Sentiment:
Positive: 3 (60.00%)
Negative: 1 (20.00%)
Neutral: 1 (20.00%)
Total: 5 (100.00%)
这是统计数据的示例代码:
def stats(tweets):
"""
Calculate and print out some basic summary statistics
INPUT:
tweets -> an array containing the analyzed tweets
"""
#init
data = {}
data['doc'] = {}
data['doc']['positive'] = 0
data['doc']['negative'] = 0
data['doc']['neutral'] = 0
data['doc']['total'] = 0
data['entity'] = {}
data['entity']['positive'] = 0
data['entity']['negative'] = 0
data['entity']['neutral'] = 0
data['entity']['total'] = 0
#loop through the tweets and count up the positive, negatives and neutrals
for tweet in tweets:
if 'entity' in tweet['sentiment']:
data['entity'][tweet['sentiment']['entity']['type']] += 1
data['entity']['total'] += 1
if 'doc' in tweet['sentiment']:
data['doc'][tweet['sentiment']['doc']['type']] += 1
data['doc']['total'] += 1
#Make sure there are some analyzed tweets
if data['doc']['total'] == 0 and data['entity']['total'] == 0:
print 'No analysis found for the Tweets'
sys.exit()
#print the stats
print ''
print ''
print '##########################################################'
print '# The Stats #'
print '##########################################################'
print ''
print ''
if data['entity']['total'] > 0:
print 'Entity-Level Sentiment:'
print 'Positive: %d (%.2f%%)' % (data['entity']['positive'], 100.0*data['entity']['positive']/data['entity']['total'])
print 'Negative: %d (%.2f%%)' % (data['entity']['negative'], 100.0*data['entity']['negative']/data['entity']['total'])
print 'Neutral: %d (%.2f%%)' % (data['entity']['neutral'], 100.0*data['entity']['neutral']/data['entity']['total'])
print 'Total: %d (%.2f%%)' % (data['entity']['total'], 100.0*data['entity']['total']/data['entity']['total'])
print ''
print ''
if data['doc']['total'] > 0:
print 'Document-Level Sentiment:'
print 'Positive: %d (%.2f%%)' % (data['doc']['positive'], 100.0*data['doc']['positive']/data['doc']['total'])
print 'Negative: %d (%.2f%%)' % (data['doc']['negative'], 100.0*data['doc']['negative']/data['doc']['total'])
print 'Neutral: %d (%.2f%%)' % (data['doc']['neutral'], 100.0*data['doc']['neutral']/data['doc']['total'])
print 'Total: %d (%.2f%%)' % (data['doc']['total'], 100.0*data['doc']['total']/data['doc']['total'])
问题描述:
我想得到积极、消极、中立的情感数据,并以JSON格式保存,这样我就可以把它们传递给Google可视化工具。我该如何制作一个包含最终统计数据(积极、消极和中立)的JSON文件呢?
1 个回答
1
import json
json_data = json.dumps(data)
如果有类似日期时间的对象,记得把它转换成字符串,这样才能被转成JSON格式。
顺便说一下:json.loads
是用来从JSON对象中读取数据的。
在这里你可以创建一个字典、列表或者Python对象。
data_list = []
temp = 'Positive: %d (%.2f%%)' % (data['entity']['positive'], 100.0*data['entity']['positive']/data['entity']['total'])
data_list.append(temp)
temp = print 'Total: %d (%.2f%%)' % (data['entity']['total'], 100.0*data['entity']['total']/data['entity']['total'])
data_list.append(temp)
其他数据也是一样的处理方式。
现在你可以把数据保存下来。
用 json.dumps(data_list)
来完成。
注意 - 我觉得你可以在以JSON格式响应后再做这些事情。这里有一个包含所有信息的数据字典,你只是把它们格式化一下,其实可以在客户端处理响应时再做。
因为字典对象里包含了所有信息,你可以只保存这个字典,然后在接收JSON响应的客户端进行格式化。
json.dumps(data)