Python按行将字符串转换为整数?

马德诺

假设我有一个这样的 Python 字符串:

Some first text:  2342
Another line here:  284
Maybe third line: 458
And forth line: 199

我想从这个字符串创建 4 个整数变量,这样 var = (第 1 行中的任何数字)。和 var2 = (第 2 行中的任何数字).. 等等。

这怎么可能?

初级压缩机

您可以将所有数字存储在一个数组中:

s = '''\
Some first text:  2342
Another line here:  284
Maybe third line: 458
And forth line: 199'''

print [int(l.split(':')[1]) for l in s.split('\n')]

输出:

[2342, 284, 458, 199]

然后数组的第一个元素将对应于第一行等

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章