Reputation: 600
I have a text file that looks like this:
A 12 YDUSD ASSDAS FSDDSFSD SDFF
AA FSDFSD FSDF SDFSDG GSDDSFS SDF
AB SDFSDF SDFFSDFDS SDSDSDSDS
ACC SDFSDDSDFSD EW12 SDFSD 3322
ACDD FDSDSFS SDFGSDG DSGSDF
AB FSDFSD SDF34 223DSFSD
ABBD 2332 ADSDFDSFDS
And so on and so fourth for about 500 different beginnings to each line. I want to write a program that will get the line, take everything from it before the first tab (there is a tab between each column) and put it into a list like this:
['A', 'AA', 'AB', 'ACC', 'ACDD', 'AB', 'ABBD']
This is my program so far but it doesn't quite work:
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
So far all it does is return an empty array, can anyone help?
Thanks!
Upvotes: 1
Views: 71
Reputation: 78800
This can be simplified. split
each line by '\t'
, and take the first element of the resulting list.
>>> with open('file.txt') as f:
... result = [line.split('\t', 1)[0] for line in f]
...
>>> result
['A', 'AA', 'AB', 'ACC', 'ACDD', 'AB', 'ABBD']
Alternatively, use result = [line[:line.find('\t')] for line in f]
.
Upvotes: 1