0

我正在运行一个 python 脚本,我试图提取其中的一部分并将其转换为 JSON 文件,以便我可以将它传递给Google Visualization

我已经包含了我的 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 格式获得积极、消极、中性的情绪,以便我可以将它们传递给谷歌可视化。如何制作包含我的最终统计数据的 JSON 文件。(正面、负面和中性)?

4

1 回答 1

1
import json

json_data = json.dumps(data)

如果有类似日期时间对象的东西,请确保将其转换为 str 以便它可以是 json 可序列化的。

仅供参考:json.loads用于从 json_object 加载它。

在这里制作一个 dict、list 或 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)

与其他 data_ques 相同。现在你可以转储数据了。json.dumps(data_list)

注意 - 我认为您可以在以 json 格式响应后执行所有这些操作。在这里,您有 data_dict 包含您只是在此处格式化它们的所有信息,而不是您可以在响应的 clint 方面进行此操作。

由于 dict 对象数据包含所有信息,因此您只能转储该信息并在接收 json 响应的客户端进行格式化。

json.dumps(data)
于 2014-04-10T19:44:45.007 回答