【问题标题】:'str' object cannot be interpreted as an integer python 3'str' 对象不能解释为整数 python 3
【发布时间】:2019-08-25 22:32:19
【问题描述】:

尝试创建一个程序来更改存储在外部 CSV 文件中的用户名,方法是将其导入然后将其写入列表并附加列表。

我最初尝试使用无效的 remove 函数,然后尝试 pop()

import csv
Usernames = []
x = int(input('How many user name u want to enter?: '))
for i in range(x):
    file = open('usernames.csv','a+')
    username = input('Enter the username: ')
    file.write(username)
    file.write('\n')
    file.close()

f = open ('usernames.csv','r')
with open ('usernames.csv','r') as fi1e:
    reader = csv.reader(fi1e)
    for row in reader:
        Username = row
        Usernames.append(Username)
print (Usernames)
f.close()

change_from = input ('What username would you like to change?: ')
Usernames.pop(change_from)

我希望它从列表中删除用户名。但相反,它说 'str' 对象不能解释为整数。

【问题讨论】:

  • 输入应该是整数(索引)还是字符串(用户名)?
  • 列表pop() 方法的documentation 表示传递给它的参数应该是整数索引,而不是字符串。请改用remove() 方法。
  • 如果您尝试从列表中删除给定的元素,请使用remove 方法。
  • 我已经尝试过使用 remove 但会说错误 Usernames.x (x) not found
  • 那么您输入的不是列表中的用户名。

标签: python python-3.x arraylist


【解决方案1】:

这是因为input 返回一个字符串。您可以使用int 将其转换为整数,如下所示:int(change_from),然后您将获得所需的类型。

【讨论】:

    【解决方案2】:

    您需要将 change_from 变量转换为 int:

    change_from = int(input('What username would you like to change?: '))
    Usernames.pop(change_from)
    

    【讨论】:

    • 如果用户输入项目的索引,这将是正确的。我怀疑这就是他们正在做的事情
    • @blitzblade 如果不是,那么为什么将变量命名为 change_from ?听起来他们希望用户输入一个索引来改变它
    • @basilisk 在这种情况下,他们不应该忽略pop 的返回值。
    • 对。但从用户的角度来看,问题是“您想更改什么用户名?:”。这实际上让我认为他们要输入用户名。
    【解决方案3】:

    您没有正确使用pop 函数。请参阅https://docs.python.org/3/tutorial/datastructures.html的文档

    list.pop([i])
    删除列表中给定位置的项目,然后 把它返还。如果未指定索引,则 a.pop() 删除并返回 列表中的最后一项。 (方法中 i 周围的方括号 签名表示参数是可选的,而不是你应该 在该位置键入方括号。你会看到这个符号 经常出现在 Python 库参考中。)

    您需要找到要删除的字符串的索引并将其传递给pop,如下所示:

    >>> a=['first', 'second', 'third']
    >>> p = a.index('second')
    >>> p
    1
    >>> a.pop(p)
    'second'
    >>> a
    ['first', 'third']
    

    【讨论】:

    • 虽然您的答案在技术上是正确的,但效率低下,因为它需要(隐式)搜索列表两次。最好通过一次调用列表remove() 方法来做到这一点。
    【解决方案4】:

    pop() 将项目的索引作为参数。在这种情况下,您应该检查用户输入的内容是否在您的列表中。如果是这样,它是什么索引?然后使用那个整数。

    change_from = input ('What username would you like to change?: ')
    if change_from in Usernames:
        Usernames.pop(Usernames.index(change_from))
    

    更新: 由于您不关心返回的内容,请使用remove()。还可以使用strip() 确保删除换行符。

    这里是完整的东西。该行是一个数组,因此您需要选择第一项。

    import csv
    Usernames = []
    x = int(input('How many user name u want to enter?: '))
    for i in range(x):
        file = open('usernames.csv','a+')
        username = input('Enter the username: ')
        file.write(username)
        file.write('\n')
        file.close()
    
    f = open ('usernames.csv','r')
    with open ('usernames.csv','r') as fi1e:
        reader = csv.reader(fi1e)
        for row in reader:
            Username = row[0]
            Usernames.append(Username.strip())
    print (Usernames)
    f.close()
    
    change_from = input ('What username would you like to change?: ')
    if change_from.strip() in Usernames:
        Usernames.remove(change_from)
    

    【讨论】:

    • 这不会从列表中删除用户名
    • 可能\n 仍附加到用户名,因此不匹配。 if 下的块是否运行?
    • 我同意使用remove() tho。这可行,但不是最有效的方式。
    • 是的,用户名被附加到列表中,例如我想用 b 替换 a 它打印出以下列表 [['a'],'b']]
    • 我明白了。让我重写答案来修复它。
    猜你喜欢
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 2020-11-13
    • 2020-04-21
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多