I have a text file which contains a table comprised of numbers e.g:
5 10 6
6 20 1
7 30 4
8 40 3
9 23 1
4 13 6
if for example I want the numbers contained only in the second column, how do i extract that column into a list?
解决方案f=open(file,"r")
lines=f.readlines()
result=[]
for x in lines:
result.append(x.split(' ')[1])
f.close()
You can do the same using a list comprehension
print [x.split(' ')[1] for x in open(file).readlines()]
Docs on split()
string.split(s[, sep[, maxsplit]])
Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argume