我有格式文件:
C1 A1
C2 A2
C3 A3
C1 A4
C4 A5
每一行都由一个标签分隔。我可以直接在array / list / tuple中获取此文件,以便可以通过索引访问每行的列吗? 像[(c1,a1),(c2,a2)..] 任何python函数都允许我们这样做吗?
答案 0 :(得分:1)
这应该做:
with open('filename.txt') as file:
result = [line.split() for line in file]
输出:
>>> with open('filename.txt') as file:
... result = [line.split() for line in file]
...
>>> result
[['C1', 'A1'], ['C2', 'A2'], ['C3', 'A3'], ['C1', 'A4'], ['C4', 'A5']]