【问题标题】:Python book example error: "string incides must be integers"Python书籍示例错误:“字符串索引必须是整数”
【发布时间】:2018-03-22 05:49:24
【问题描述】:

脚本语言:Python 3.6

参考书:Python 数据可视化食谱 [Milovanović 2013-11-25]


自学 Python 数据可视化

当我从书中执行代码时

import requests

url = 'https://github.com/timeline.json'

r = requests.get(url)
json_obj = r.json()

repos = set() # we want just unique urls
for entry in json_obj:
    try:
        repos.add(entry['repository']['url'])
    except KeyError as e:
        print ("No key %s. Skipping..." % (e))

from pprint import pprint 
pprint(repos)

我得到错误

repos.add(entry['repository']['url'])
TypeError: string indices must be integers

如何排除故障?当我看到similar threads 时,我画了一个空白

书中的代码是否正确?

[顺便说一句,repos = set() 中的 set() 是从哪里来的?]

请指出正确的方向

【问题讨论】:

  • 来自 json_obj:你好,远行的陌生人。如果您正在阅读本文,那么您可能在几年前没有看到我们的博客文章宣布此 API 将消失:git.io/17AROg 不要害怕,您应该能够从闪亮的新事件 API 中获得所需的东西而是。

标签: python typeerror


【解决方案1】:

如果你打印 json_obj 你会得到这个:

{'message': 'Hello there, wayfaring stranger. If you’re reading this then you pr
obably didn’t see our blog post a couple of years back announcing that this API 
would go away: http://git.io/17AROg Fear not, you should be able to get what you
 need from the shiny new Events API instead.', 'documentation_url': 'https://dev
eloper.github.com/v3/activity/events/#list-public-events'}

所以这个链接似乎是旧的,你必须查找新的。

关于你的第二个问题: set() 是一个类似于dict()list() 的数据容器。集合类似于列表,因为它们存储了许多对象。最大的区别是:

  • 集合未排序(如字典)
  • 套装只包含独特的物品

您可以在 python 文档中找到更多信息: https://docs.python.org/3/tutorial/datastructures.html#sets

希望对你有所帮助,祝你学习顺利。

【讨论】:

    【解决方案2】:

    正在使用的 API 已过时。以下代码使用当前 API:

    import requests
    url = 'https://api.github.com/events' # New API URL
    
    r = requests.get(url)
    json_obj = r.json()
    
    repos = set() # we want just unique urls
    for entry in json_obj:
        try:
            repos.add(entry['repo']['url'])  # Key change. 'repo' not 'repository'
        except KeyError as e:
            print ("No key %s. Skipping..." % (e))
    
    from pprint import pprint 
    pprint(repos)
    

    正如其他人指出的那样,set() 创建了一个集合对象,该对象只能包含唯一值。示例:

    >>> set([1,2,3,4,4,5,5,6,6])
    {1, 2, 3, 4, 5, 6}
    

    请注意,集合是无序的,因此不要像示例中那样依赖被排序的项目。

    【讨论】:

      【解决方案3】:

      只是为了让您得到一些问题的答案...

      TypeError: string indices must be integers 是因为,由于 API 已关闭,entry 现在只是一个字符串 (u'documentation_url'),而当 entry['repository'] 引发错误时,因为使用字符串,您只能使用 get 整数 n 中的第 n 个字符(您无法获取 repository-th 字符)。

      [顺便说一句,repos = set() 中的 set() 是从哪里来的?]

      当您执行repos = set() 时,您只是在创建一个空集对象并将其分配给repos。稍后您将使用repos.add(entry['repository']['url']) 填写它

      【讨论】:

        【解决方案4】:

        您尝试访问的 entry 对象是一个字符串,因此您无法使用非整数索引访问它。我尝试运行您的代码,但由于请求过多,该 url 似乎已关闭或被阻止,因此这可能是 entry 最终成为字符串对象的原因。

        repos = set() 意味着,当您向repos 添加新网址时,它将忽略该网址已经在集合中的情况,这样您就不会出现重复。如果您改用repos = [],则必须在每次插入时手动检查重复项(除非您想允许重复项)。

        您可以在此处阅读有关 set() 数据结构的更多信息:https://docs.python.org/3/tutorial/datastructures.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-30
          • 2021-09-20
          • 2012-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-26
          • 2016-03-26
          相关资源
          最近更新 更多