Python JSON解析不存储值

时间:2015-08-10 00:16:47

标签: python json elasticsearch

我已经多次查看过了,似乎无法找到它的问题。

我试图从JSON响应中拉出3个字段(约定,分享,评论),将它们加在一起,然后打印总和。

它似乎正确地返回字段,但它在最终打印中返回零。

我对这些东西很新,但感谢任何人都可以给我的帮助。我猜这里有一些基本的东西。

import urllib2,time,csv,json,requests,urlparse,pdb


SEARCH_URL = urllib2.unquote("http://soyuz.elastic.tubularlabs.net:9200/intelligence/video/_search?q=channel_youtube_id:""%s""%20AND%20published:%3E20150715T000000Z%20AND%20published:%3C20150716T000000Z")

reader = csv.reader(open('input.csv', 'r+U'), delimiter=',', quoting=csv.QUOTE_NONE)
cookie = {"user": "2|1:0|10:1438908462|4:user|36:eyJhaWQiOiA1Njk3LCAiaWQiOiA2MzQ0fQ==|b5c4b3adbd96e54833bf8656625aedaf715d4905f39373b860c4b4bc98655e9e"}

# idsToProcess = []
# for row in reader:
#   if len(row)>0:
#       idsToProcess.append(row[0])
idsToProcess = ['qdGW_m8Rim4FeMM29keDEg']
for userID in idsToProcess:
#   print "fetching for %s.." % fbid
url = SEARCH_URL % userID
soyuzResponse = None
response = requests.request("GET", url, cookies=cookie)
ret = response.json()
soyuzResponse = ret['hits']['hits'][0]['_source']['youtube']
print soyuzResponse
totalDelta = 0
totalEngagementsVal = 0
totalSharesVal = 0
totalCommentsVal = 0
valuesArr = []
for entry in valuesArr:
        arrEngagements = entry['engagements']
        arrShares = entry['shares'] 
        arrComments = entry['comments'] 

        if len(arrEngagements)>0:
            totalEngagementsVal = arrEngagements
        elif len(arrShares)>0:
            totalSharesVal = arrShares
        elif len(arrComments)>0:
            totalCommentsVal = arrComments

print "%s,%s" % (userID,totalEngagementsVal+totalSharesVal+totalCommentsVal)
totalDelta += totalEngagementsVal+totalSharesVal+totalCommentsVal
time.sleep(0)
print "%s,%s" % (userID,totalDelta)




exit()

这是我正在解析的json:

   took: 371,
    timed_out: false,
    _shards: {
    total: 128,
    successful: 128,
    failed: 0
    },
    hits: {
    total: 1,
    max_score: 9.335125,
    hits: [
    {
    _index: "intelligence_v2",
    _type: "video",
    _id: "jW7mjVdzR_U",
    _score: 9.335125,
    _source: {
    claim: [
    "Blucollection%2Buser"
    ],
    topics: [
    {
    title_non: "Toy",
    topic_id: "/m/0138tl",
    title: "Toy"
    },
    {
    title_non: "Sofia the First",
    topic_id: "/m/0ncq483",
    title: "Sofia the First"
    }
    ],
    likes: 1045,
    duration: 318,
    channel_owner_type: "influencer",
    category: "Entertainment",
    imported: "20150809T230652Z",
    title: "Princess Sofia Cash Register Toy Surprise - Play Doh Caja Registradora             Disney Sofia the First",
    audience_location: [
    {
    country: "US",
    value: 100
    }
    ],
    comments: 10,
    twitter: {
    tweets: 6,
    engagements: 6
    },
    description: "Disney Princess "Sofia Cash Register" toy unboxing review by          DisneyCollector. This is the authentic Royal toy of Sofia fit for a little     Princess of Enchantia. Young Girls learn early on how mathematics is important in our lives, and learn to do math, developing creativity with a super fun game! Thx 4 watching this "Disney Princess Sofia cash register" unboxing review. In this video i also used Disney Frozen Princess Anna, Nickelodeon Peppa Pig blind bag and plastilina Play-Doh. Revisión del juguete Princesita Sofía Caja Registradora Real para niños y niñas. Las niñas aprenden desde muy temprano cómo las matemáticas es importante en nuestras vidas, y aprenden a hacer matemáticas, el desarrollo de la creatividad con un juego súper divertido! Here's how to say Princess in other languages: printzesa, 公主, prinses, prenses, printsess, princesse, Prinzessin, puteri, banphrionsa, Principesse, principessa, プリンセス, princese, puteri, prinsessa,prinsesse, princesa, công chúa, tywysoges, Princesses Disney, Prinzessinen, 공주, Princesas Disney, Disney πριγκίπισσες, Дисней принцесс, 디즈니 공주, ディズニーのお姫様, Vorstin, koningsdochter, Fürstin, πριγκίπισσα, księżniczka, królewna, принцесса. Here's how register is called in other languages: Caja Registradora de Princesa Sofía, Caisse Enregistreuse Princesse Sofia, Kassa, Registrierkasse Sofia die Erste Auf einmal Prinzessin, Registratore di Cassa di La Principessa Sofia, Caixa Registadora da Princesa Sofia, ηλεκτρονική ταμειακή μηχανή Σοφία η Πριγκίπισσα, 電子式金銭登録機 ちいさなプリンセス ソフィア, София Прекрасная кассовый аппарат, 디즈니주니어 리틀 프린세스 소피아 전자 금전 등록기, máy tính tiền điện tử, daftar uang elektronik, elektronik yazarkasa, Sofia den första kassaapparat leksak, Jej Wysokość Zosia kasa zabawki, Sofia het prinsesje kassa speelgoed, София Първа касов апарат играчка, casa de marcat jucărie Sofia Întâi. Princess Sofia SLEEPOVER Slumber Party - Princesita Sofía Pijamada Real. https://www.youtube.com/watch?v=WSa-Tp7HfyQ Princesita Sofía Castillo Mágico Parlante juguete de niñas. https://www.youtube.com/watch?v=ALQm_3uhIyg Sofia the First Magical Talking Castle Royal Prep Academy. https://www.youtube.com/watch?v=gcUiY0Suzrc Play-Doh Meal Makin' Kitchen with Princess Sofia the First. https://www.youtube.com/watch?v=x_-OxnRXj6g Sofia the First Royal Prep Academy Dolls Character Collection. https://www.youtube.com/watch?v=_kNY6AkSp9g Peppa Pig Picnic Adventure Car With Princess Sofia the First. https://www.youtube.com/watch?v=KIPH3txlq1o Watch "Sofia the First Talking Magic Castle" talking Clover: https://www.youtube.com/watch?v=ALQm_3uhIyg Play-Doh Sofia the First Magic Talking Castle w/ Peppa Pig: https://www.youtube.com/watch?v=-slXqMiDrY0 Play-Doh Sofia the First Going to School Portable Classroom http://www.youtube.com/watch?v=0R-dkVAIUlA",
views: 941726,
channel_network: null,
channel_subscribers: 5054024,
youtube_id: "jW7mjVdzR_U",
facebook: {
engagements: 9,
likes: 2,
shares: 7,
comments: 0
},
location_demo_count: 1,
is_public: true,
engagements: 1070,
channel_country: "US",
demo_count: null,
monetizable: true,
youtube: {
engagements: 1055,
likes: 1045,
comments: 10
},
published: "20150715T100003Z",
channel_youtube_id: "qdGW_m8Rim4FeMM29keDEg"
}
}
]
}
}

运行脚本后来自终端的响应:

{u'engagements': 1055, u'likes': 1045, u'comments': 10}
qdGW_m8Rim4FeMM29keDEg,0
qdGW_m8Rim4FeMM29keDEg,0

1 个答案:

答案 0 :(得分:1)

你的问题是这两行:

valuesArr = []
for entry in valuesArr:

由于valuesArr为空,因此for循环永远不会迭代,而且这是总计汇总的位置。