如何解决Python中的字符串整数问题

Santosh Agrawal

我需要帮助来修复我的代码。我正在尝试从名为output的文件中打印字符串的一部分。

with open('output.txt') as f:
for line in f:
    if 'UserID' in line:
        print (type(line))  
        local_var1 = line.index('UserID=\"')
        print (type(local_var1))
        #print (line)
        local_var2 = line.index('\" EntityID')
        print (type(local_var2))
        local_var3 = (line[local_var1,local_var2])
        print (local_var3)

但是我收到以下错误。我已经确认我的local_var1和local_var2都是整数。

<class 'str'>
<class 'int'>
<class 'int'>
Traceback (most recent call last):
  File "output.py", line 10, in <module>
    local_var3 = (line[local_var1,local_var2])
TypeError: string indices must be integers

文件输出:

<User UserID="test" EntityID="000" FirstName="test1" LastName="test2" Email1="" Email2="" Email3="" />
big_bad_bison

如果要获取子字符串,请使用“:”,而不是“,”。

local_var3 = (line[local_var1:local_var2])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何解决for循环中的字符串索引必须是整数问题以将字符串中的每个单词大写

Python如何解决字符串中的Unicode错误

如何解决这个尝试迭代字符串的问题?

如何解决这个关于字符串的函数问题

如何解决错误字符串索引在文本扩展中必须为整数

我如何解决这个电子邮件字符串问题?(PYTHON)

如何解决此cmdlet中的输入字符串问题?

如何解决Pojo中字符串/日期字段数据类型转换的问题?

如何解决从mmap()返回的字符串中缺少NUL终止符的问题?

如何解决Angular8中输入字符串格式不正确的问题?

在反应中,如何解决将字符串转换为对象的问题?

类型错误:字符串索引必须是整数;如何在我的代码中解决这个问题?

如何解决字符串中的问号

如何解决python中的“IndexError:字符串索引超出范围”错误?

在删除Python DataFrame中的字符串部分时,如何解决错误?

Python 如何解决字符串中的不等式?

为什么Cookie或传递给PHP的数据字符串中缺少“ +”字符,以及如何解决这个问题

Python的Robot Framework关键字:字符串或IP不匹配时出错。如何解决这个问题?

如何解决打字稿中“对象”类型上不存在的“属性”字符串问题

使用cleaned_data时如何解决“ TypeError:字符串索引必须为整数”?

如何解决“ TypeError:字符串索引必须为整数”错误?

如何解决此JSON TypeError:字符串索引必须为整数?

如何解决将字符串转换为日期时间的问题?

我的输入始终以字符串而不是int的形式读取,如何解决此问题

如何解决“连接字符串格式不正确”的问题?

如何解决“类型‘字符串’不可分配给‘从不’类型”的问题?

如何解决这个问题(数组到字符串的转换)

如何解决“WP_Term 类的对象无法转换为字符串”的问题?

如何解决这个字符串长度问题?