我已使用以下方法从字符串中删除某个尾随字符串“lbs”。但是,当我运行代码并打印出结果时,没有任何更改,字符串也没有被删除。非常感谢您在这种情况下的帮助!谢谢你的帮助
Data:
**Weights:**
0 159lbs
1 183lbs
2 150lbs
3 168lbs
4 154lbs
**Code:**
# Converting the Weights to an Integer Value from a String
for x in Weights:
if (type(x) == str):
x.strip('lbs')
**Output:**
Weights:
0 159lbs
1 183lbs
2 150lbs
3 168lbs
4 154lbs
您正在从字符串中剥离值,但未将其保存在列表中。 尝试使用数字索引,如下所示:
但是,如果您不习惯使用范围循环,可以按如下方式枚举:
这对你有帮助吗
这里我没有使用
isinstance(line, str)
来显式检查行是否是string
,因为行号是integer
(我想)在这里,我把数据作为一个txt文件
相关问题 更多 >
编程相关推荐