with open('/path/to/file') as myfile: # Open the file
data = [] # Make a list to hold the data
for line in myfile: # Loop through the lines in the file
data.append(line.split(None, 2)[1]) # Get the data and add it to the list
print (data) # Print the finished list
with open('file.txt') as infile:
result = []
for line in infile: #loop through the lines
data = line.split(None, 2)[1] #split, get the second column
result.append(data) #append it to our results
print data #Just confirming
这将起作用:
这里的重要部分是:
^{} ,它根据空格分隔行。
with-statement,完成后自动关闭文件。
请注意,您还可以使用list comprehension:
可以使用^{} 拆分行:
相关问题 更多 >
编程相关推荐