我想使用split string方法将每一行的信息提取到一个列表中。
发布于 2017-06-01 06:22:10
使用拆分线,这样会更好:
file = open('scores.txt','r').read().splitlines()
exam_one = []
for line in file:
line = line.split() # not strip
exam_one.append(int(line[2])) # or better use float() since it's an exam
print(exam_one) # => [100, 82, 94, 89, 87]发布于 2017-06-01 06:39:07
我不知道你的档案是怎样的,但我认为它是这样的:
Hopper, Grace 100 98 87 97
Knuth, Donald 82 87 92 81
Goldberg, Adele 94 96 90 91
Kernighan, Brian 89 74 89 77
Liskov, Barbara 87 97 81 85我也不明白你想要什么样的输出,但我认为是这样的:
[['Hopper,', 'Grace', '100', '98', '87', '97'], ['Knuth,', 'Donald', '82', '87', '92', '81'], ['Goldberg,', 'Adele', '94', '96', '90', '91'], ['Kernighan,', 'Brian', '89', '74', '89', '77'], ['Liskov,', 'Barbara', '87', '97', '81', '85']]我已经开发了这一行代码(适用于 3.6):
with open('scores.txt', 'r') as file:
print([[value for value in line.strip().replace(',','').split()] for line in file])与:相同
with open('scores.txt', 'r') as file:
tmp = []
for line in file:
tmp.append(line.strip().replace(',','').split())
# Also you can delete tmp = [] and replace the tmp.append(...) line to tmp = [var for var in line.strip().replace(',','').split()]
print(tmp)输出:
[['Hopper,', 'Grace', '100', '98', '87', '97'], ['Knuth,', 'Donald', '82', '87', '92', '81'], ['Goldberg,', 'Adele', '94', '96', '90', '91'], ['Kernighan,', 'Brian', '89', '74', '89', '77'], ['Liskov,', 'Barbara', '87', '97', '81', '85']]与:相同
[
['Hopper,', 'Grace', '100', '98', '87', '97'],
['Knuth,', 'Donald', '82', '87', '92', '81'],
['Goldberg,', 'Adele', '94', '96', '90', '91'],
['Kernighan,', 'Brian', '89', '74', '89', '77'],
['Liskov,', 'Barbara', '87', '97', '81', '85']
]我使用了like和output print(),但您可以根据需要定义变量。
PD:我找到了一个更简单的解决方案:
with open('scores.txt', 'r') as file:
print([line.split() for line in file.read().replace(',','').splitlines()])发布于 2017-06-01 06:40:24
假设您有以下字符串,其中包含单词(由水平空格分隔)和行(由\n或垂直空格分隔):
>>> print(data)
Hopper, Grace 100 98 87 97
Knuth, Donald 82 87 92 81
Goldberg, Adele 94 96 90 91
Kernighan, Brian 89 74 89 77
Liskov, Barbara 87 97 81 85如果你仅仅使用.split(),你就失去了行和词之间的所有区别:
>>> data.split()
['Hopper,', 'Grace', '100', '98', '87', '97', 'Knuth,', 'Donald', '82', '87', '92', '81', 'Goldberg,', 'Adele', '94', '96', '90', '91', 'Kernighan,', 'Brian', '89', '74', '89', '77', 'Liskov,', 'Barbara', '87', '97', '81', '85']为了保持这种差异,您需要将.splitlines()与.split()结合使用
>>> [line.split() for line in data.splitlines()]
[['Hopper,', 'Grace', '100', '98', '87', '97'], ['Knuth,', 'Donald', '82', '87', '92', '81'], ['Goldberg,', 'Adele', '94', '96', '90', '91'], ['Kernighan,', 'Brian', '89', '74', '89', '77'], ['Liskov,', 'Barbara', '87', '97', '81', '85']]同样的概念也适用于从文件读取的数据。您可以使用for循环遍历文件的各个行,而不是使用.splitlines():
>>> with open('/tmp/file.txt') as f:
... for line in f:
... print(line.split())
...
['Hopper,', 'Grace', '100', '98', '87', '97']
['Knuth,', 'Donald', '82', '87', '92', '81']
['Goldberg,', 'Adele', '94', '96', '90', '91']
['Kernighan,', 'Brian', '89', '74', '89', '77']
['Liskov,', 'Barbara', '87', '97', '81', '85']或者,如果您想要嵌套列表:
>>> with open('/tmp/file.txt') as f:
... print([line.split() for line in f])
...
[['Hopper,', 'Grace', '100', '98', '87', '97'], ['Knuth,', 'Donald', '82', '87', '92', '81'], ['Goldberg,', 'Adele', '94', '96', '90', '91'], ['Kernighan,', 'Brian', '89', '74', '89', '77'], ['Liskov,', 'Barbara', '87', '97', '81', '85']]如果您只想要这些行中的一个数字:
>>> with open('/tmp/file.txt') as f:
... print([line.split()[2] for line in f])
...
['100', '82', '94', '89', '87']使用Python循环或列表理解打开文件并遍历各行的形式被认为是一种重要的for习惯用法。使用它们,而不是将整个文件读取到内存中。
https://stackoverflow.com/questions/44295500
复制相似问题