我想使用拆分字符串方法将每行的信息提取到列表中。
答案 0 :(得分:1)
使用分界线,它更好:
operator=()
答案 1 :(得分:1)
我不知道你的文件是怎么回事,但我认为它是这样的:
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']]
我开发了这个单行代码(对于python 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和输出print()
,但你可以定义一个你想要的变量。
PD:我找到了一个更简单的解决方案:
with open('scores.txt', 'r') as file:
print([line.split() for line in file.read().replace(',','').splitlines()])
答案 2 :(得分:1)
假设您有以下字符串,其中包含单词(由水平空格分隔)和行(由\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']]
同样的概念适用于从文件读取的数据。您可以使用.splitlines()
循环遍历文件的各行,而不是使用for
:
>>> 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']
打开文件并使用for
循环或列表理解循环遍历的形式被认为是一种重要的Python习语。使用它们而不是将整个文件读入内存。
答案 3 :(得分:0)
不要先将整个文件读入内存。文件对象是迭代器。
result = []
with open('scores.txt') as f:
for line in f:
# E.g., fields == ['Hopper,', 'Grace', '100', '98', '87', '97']
fields = line.strip().split()
目前尚不清楚你想要什么作为最终结果;也许是每一行的一年级?拆分线后,您可以使用
result.append(fields[2])