简体   繁体   English

如何修复“列表索引必须为整数或切片”错误

[英]How to fix “list indices must be integers or slices” error

I'm making a program that should save your ip address to a list in a separate file using shelve. 我正在制作一个程序,应该将您的IP地址保存到一个单独文件的列表中。 Each time it is opened it should put the the ip address in list "pubipcount" in list "pubiplist". 每次打开它时,都应将IP地址放在列表“ pubiplist”中的列表“ pubipcount”中。 When I go to add a new ip I get an error that I don't understand. 当我添加一个新的IP时,出现一个我不理解的错误。

I've only ran the code bit by bit to find the part that is broken, and I've taken the liberty of narrowing it down to only the information you need to help me. 我只是一点一点地运行了代码,以找到损坏的部分,而我自由地将其范围缩小到仅需要帮助我的信息。 I've already looked online for a possible answer but I haven't found anything helpful to me. 我已经在网上寻找可能的答案,但没有发现任何对我有帮助的东西。

import shelve
import socket
import urllib.request

save = shelve.open('ip_whitelist', writeback=True)

hostname = socket.gethostname()
PubIP = urllib.request.urlopen('https://ident.me').read().decode('utf8')
LocIP = socket.gethostbyname(hostname)

save['pubipcount'] = 0
save['pubiplist'] = []

save['pubipcount'] = save['pubipcount'] + 1
save['pubiplist']['pubipcount'] = PubIP

This is the exact error: 这是确切的错误:

Traceback (most recent call last):
  File "C:\Users\brenn\Desktop\IP\ip.py", line 27, in <module>
    save['pubiplist']['pubipcount'] = PubIP
TypeError: list indices must be integers or slices, not str

The problem is ['pubipcount'] . 问题是['pubipcount'] In the context it is just a string. 在上下文中,它只是一个字符串。 I believe you want: 我相信你想要:

save['pubiplist'][save['pubipcount']] = PubIP

This way you are referring to the value in save['pubipcount'] instead of the string 'pubipcount' as the index. 这样,您将引用save['pubipcount']而不是字符串'pubipcount'作为索引。

Edit: This will likely result in an IndexError, so using append() is better 编辑:这可能会导致IndexError,所以使用append()更好

save['pubiplist'].append(PubIP)

if you use a list the index must be integers such as save['pubiplist'][0] 如果您使用list则索引必须为整数,例如save['pubiplist'][0]

if you want a str as key, you can use a dict instead, change save['pubiplist'] = [] to save['pubiplist'] = {} 如果您想使用str作为键,则可以使用dict ,将save['pubiplist'] = []更改为save['pubiplist'] = {}


for your situation, I suggest you to use PubIP as key, and count PubIP separately: 根据您的情况,建议您使用PubIP作为密钥,并分别计算PubIP

from collections import defaultdict
# do this only once
save['pubiplist'] = defaultdict(int)

# recieve IP repeately

# update IP and count each time
save['pubiplist'][PubIP] += 1

Shelve allows you use strings as keys but you can't use them as sub-indexes. 搁置允许您将字符串用作键,但不能将它们用作子索引。

The quickest fix would be to interpolate the count into the key: 最快的解决方法是将计数插入到密钥中:

save[f'pubiplist-{pubipcount}'] = PubIP

暂无
暂无

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

相关问题 如何解决“列表索引必须是整数或切片,而不是列表”错误? - How to fix ' list indices must be integers or slices, not list' error? 如何修复错误“元组索引必须是整数或切片,而不是列表” - how to fix error "tuple indices must be integers or slices, not list" 如何修复此错误:列表索引必须是整数或切片,而不是元组 - How to fix this error: list indices must be integers or slices, not tuple 如何解决:“列表索引必须是整数或切片,而不是 str” - how to fix: “list indices must be integers or slices, not str” 如何修复列表索引必须是整数或切片,而不是 str - how to fix list indices must be integers or slices, not str 如何修复“类型错误:列表索引必须是整数或切片,而不是元组” - How to fix "TypeError: list indices must be integers or slices, not tuple" 如何修复“TypeError: list indices must be integers or slices, not str.”? - How to fix " TypeError: list indices must be integers or slices, not str. "? 我该如何解决这个错误? TypeError:列表索引必须是整数或切片,而不是 str - how I can fix this error? TypeError: list indices must be integers or slices, not str 如何修复错误TypeError:列表索引必须是整数或切片,而不是python中的str - how to fix error TypeError: list indices must be integers or slices, not str in python 我该如何解决此错误? TypeError:列表索引必须是整数或切片,而不是str:discord.py - How do i fix this error? TypeError: list indices must be integers or slices, not str: discord.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM