简体   繁体   English

python json错误:字符串索引必须是整数

[英]python json errors:string indices must be integers

Hello when I'm learning python I was blocked by the question below, I've tried some solutions on the site by none of them works so far. 您好,当我学习python时,我被下面的问题阻止了,到目前为止,我已经在网站上尝试了一些解决方案,但这些解决方案都无效。 When I compile, it returns me: string indices must be integers. 当我编译时,它返回我:字符串索引必须是整数。 Can you help me with that? 你能帮我吗? Thank you. 谢谢。

import json
import urllib

url = raw_input('Enter location: ')
uh = urllib.urlopen(url)
data = uh.read()

print 'Retrieving', url
print 'Retrieved', len(data), 'characters'
info = json.loads(data)
#print 'User count:', len(info)
s = 0

for item in info:
    print item["comments"]["count"]

The url includes something like this 网址中包含类似这样的内容

{
  "note":"This file contains the sample data for testing",
  "comments":[
    {
      "name":"Romina",
      "count":97
    },
    {
      "name":"Laurie",
      "count":97
    },
    {
      "name":"Bayli",
      "count":90
    },
    {
      "name":"Siyona",
      "count":90
    },
    {
      "name":"Taisha",
      "count":88
    },
    {
      "name":"Ameelia",
      "count":87
    },}

Your for loop doesn't match the structure of the data. 您的for循环与数据的结构不匹配。

Try this: 尝试这个:

for item in info["comments"]:
    print item["count"]

If you want to see the association of names to counts, try: 如果要查看名称与计数的关联,请尝试:

for item in info["comments"]:
    print item["name"], item["count"]

If you want to see the names, counts, and their location in that list, try: 如果要查看名称,计数及其在该列表中的位置,请尝试:

for index, item in enumerate(info["comments"]):
    print index, item["name"], item["count"]

You should use print item["comments"][0]["count"] or print item["comments"][1]["count"] and so on. 您应该使用print item["comments"][0]["count"]print item["comments"][1]["count"] ,依此类推。 You should use the index of the slice then search for the "count" value from the dicts. 您应该使用切片的索引,然后从字典中搜索"count"值。

You can, also, use something like this: 您也可以使用以下内容:

for item in info["comments"]:
    print item["count"]

You iterate through every value in parsed dictionary, ie your item s in info are 您遍历解析字典中的每个值,即info中的item

  • "This file contains the sample data for testing"
  • [ { "name":"Romina", "count":97 }, { "name":"Laurie", "count":97 }, { "name":"Bayli", "count":90 }, { "name":"Siyona", "count":90 }, { "name":"Taisha", "count":88 }, { "name":"Ameelia", "count":87 }, ... ]

Therefore, in first iteration, you try to get ["comments"]["count"] from the actual string. 因此,在第一次迭代中,您尝试从实际字符串中获取["comments"]["count"]

If you want to print counts for each comments element, then you should rather: for item in info["comments"]: print item["count"] 如果要打印每个注释元素的计数,则应改为: for item in info["comments"]: print item["count"]

The best way of debugging is, just use interpreter (IPython or Jupyter notebooks are very good for this), assign the page to variable, or even create a mock one with the same structure, and then parse it, to verify the correctness of your code. 调试的最佳方法是,仅使用解释器(IPython或Jupyter笔记本非常适合此操作),将页面分配给变量,甚至创建具有相同结构的模拟对象,然后对其进行解析以验证您的正确性。码。 Here the problem is the way the code is being parsed. 这里的问题是代码的解析方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM