我有一个看起来像这样的文本文件:
12 YDUSD ASSDAS FSDDSFSD SDFF
AB SDFSDF SDFFSDFDS SDSDSDSDS
ACC SDFSDDSDFSD EW12 SDFSD 3322
ACDD FDSDSFS SDFGSDG DSGSDF
AB FSDFSD SDF34 223DSFSD
ABBD 2332 ADSDFDSFDS
依此类推第四,每行大约500个不同的开头。我想编写一个程序来获取该行,在第一个选项卡之前从中获取所有内容(每列之间有一个选项卡)并将其放入如下列表中:
[' A',' AA',' AB',' ACC',' ACDD',& #39; AB',' ABBD']
到目前为止,这是我的计划,但它并不起作用:
file1 = open("filename", "r")
file2 = open("filename2", "w")
i=0
k = 0
sp500list = []
with open("filename1") as f:
lines = f.readlines()
while (abc < len(lines)):
LineStr = str(lines[i])
j = 0
if (LineStr[j] != ''):
j = j + 1
if (LineStr[j] !=''):
j = j + 1
elif (LineStr[j] == ' '):
sp500list.append(str(LineStr[:2]))
i = i + 1
if (LineStr[j] !=''):
j = j + 1
elif (LineStr[j] == ' '):
sp500list.append(str(LineStr[:3]))
i = i + 1
if (LineStr[j] !=''):
sp500list.append(str(LineStr[:4]))
i = i + 1
j = 0
elif (LineStr[j] == ' '):
i = i + 1
print sp500list
abc = abc + 1
到目前为止它所做的只是返回一个空数组,任何人都可以帮忙吗?
谢谢!
答案 0 :(得分:1)
这可以简化。 split
每行'\t'
,并获取结果列表的第一个元素。
>>> with open('file.txt') as f:
... result = [line.split('\t', 1)[0] for line in f]
...
>>> result
['A', 'AA', 'AB', 'ACC', 'ACDD', 'AB', 'ABBD']
或者,使用result = [line[:line.find('\t')] for line in f]
。