我的csv文件在单元格中有制表符分隔的数字。
我想将行中的所有值转换为数组中的数字。
例如: 输入:
1 2 3 4
0 1 1 3
输出:
[1234, 0113]
怎么做?
答案 0 :(得分:0)
如果要删除每一行中的所有空格,可以使用正则表达式:
import re
lines = list()
with open('input.txt', 'r') as istr:
for line in istr:
line = re.sub(r'\s*', '', line)
lines.append(line)
或者,如果你喜欢它的功能:
import re
with open('input.txt', 'r') as istr:
lines = [re.sub(r'\s*', '', line) for line in istr]
请注意,上面的示例将为您提供字符串列表。如果要将它们转换为整数,可以将int
内置函数映射到它上面。
numbers = [int(n) for n in lines]
答案 1 :(得分:0)
你不需要re
,只需split
,str.join
并映射到int
:
with open("in.txt") as f:
nums = map(int,("".join(x.rstrip().split()) for x in f))
或者不使用map
:
[int(s) for s in (x.rstrip().replace(" ","") for x in f)]
使用100行的文件:
In [49]: %%timeit
with open("in.txt") as f:
nums = map(int,("".join(x.rstrip().split()) for x in f))
....:
10000 loops, best of 3: 140 µs per loop
In [50]: %%timeit
with open('in.txt', 'r') as istr:
lines = [re.sub(r'\s*', '', line) for line in istr]
[int(n) for n in lines]
....:
1000 loops, best of 3: 519 µs per loop
In [53]: %%timeit
....: with open("in3.txt") as f:
....: nums = [int(s) for s in (x.rstrip().replace(" ","") for x in f)]
....:
10000 loops, best of 3: 127 µs per loop