如何使用python和文件解析从每行中获得第二件事
我正在尝试解析具有以下结构的文件:如何使用python和文件解析从每行中获得第二件事,python,file,parsing,Python,File,Parsing,我正在尝试解析具有以下结构的文件: 0 rs41362547 MT 10044 1 rs28358280 MT 10550 ... 以此类推,我想把每行中的第二个东西放到一个数组中。我知道这应该很容易,但经过多次搜索,我还是迷路了。我对python真的很陌生,做这件事的脚本是什么 谢谢 您可以使用以下方法拆分线: 可以使用以下方法拆分线: 这将有助于: with open('/path/to/file') as myfile: # Open the file
0 rs41362547 MT 10044
1 rs28358280 MT 10550
...
以此类推,我想把每行中的第二个东西放到一个数组中。我知道这应该很容易,但经过多次搜索,我还是迷路了。我对python真的很陌生,做这件事的脚本是什么
谢谢 您可以使用以下方法拆分线:
可以使用以下方法拆分线: 这将有助于:
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('/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
这里的重要部分是:
,它根据空格分隔行
,完成后将自动为您关闭文件
请注意,您还可以使用:
看一看功能看一看function@user3078700-很高兴能帮忙!请不要忘记接受我的答案。单击检查,让人们知道这个问题已经解决。@user3078700-很高兴能提供帮助!请不要忘记接受我的答案,单击检查让人们知道此问题已解决。
with open('/path/to/file') as myfile:
data = [line.split(None, 2)[1] for line in myfile]
print (data)