我有一个看起来像这样的文本文件:
1
Subpop 0 best fitness of run: Fitness: Standardized=73.0 Adjusted=0.013513513513513514 Hits=16
2
Subpop 0 best fitness of run: Fitness: Standardized=61.0 Adjusted=0.016129032258064516 Hits=28
3
Subpop 0 best fitness of run: Fitness: Standardized=73.0 Adjusted=0.013513513513513514 Hits=16
4
Subpop 0 best fitness of run: Fitness: Standardized=70.0 Adjusted=0.014084507042253521 Hits=19
5
Subpop 0 best fitness of run: Fitness: Standardized=72.0 Adjusted=0.0136986301369863 Hits=17
6
Subpop 0 best fitness of run: Fitness: Standardized=67.0 Adjusted=0.014705882352941176 Hits=22
7
Subpop 0 best fitness of run: Fitness: Standardized=65.0 Adjusted=0.015151515151515152 Hits=24
8
Subpop 0 best fitness of run: Fitness: Standardized=73.0 Adjusted=0.013513513513513514 Hits=16
9
Subpop 0 best fitness of run: Fitness: Standardized=78.0 Adjusted=0.012658227848101266 Hits=11
10
Subpop 0 best fitness of run: Fitness: Standardized=65.0 Adjusted=0.015151515151515152 Hits=24
我正在尝试使用Python从"标准化"中提取数字。和" Hits"每行的部分并将它们放在各自独立的列表中,但我不熟悉从Python中读取文件。最好的方法是什么?
答案 0 :(得分:2)
我们通常不会为人们编写代码,但这看起来可能不是作业。我还想说明一点。
文件是以换行符结尾的字符串。以换行符结尾的字符串列表也是可迭代的。所以从开始开始,当代码在代码列表中工作时,切换到打开的文件。在我看来,不这样做是一个很大的错误和问题根源。
接下来,迭代并抛出“垃圾”行。然后解析支付线并对提取的数据进行任何处理。解析取决于问题。我在下面选择使用分割线和分割方法。
file = '''\
1
Subpop 0 best fitness of run: Fitness: Standardized=73.0 Adjusted=0.013513513513513514 Hits=16
2
Subpop 0 best fitness of run: Fitness: Standardized=61.0 Adjusted=0.016129032258064516 Hits=28
3
Subpop 0 best fitness of run: Fitness: Standardized=73.0 Adjusted=0.013513513513513514 Hits=16
4
Subpop 0 best fitness of run: Fitness: Standardized=70.0 Adjusted=0.014084507042253521 Hits=19
5
Subpop 0 best fitness of run: Fitness: Standardized=72.0 Adjusted=0.0136986301369863 Hits=17
'''.splitlines(keepends=True)
stand = []
hits = []
for line in file:
if len(line) < 50:
continue
fields = line.split('=')
stand.append(float(fields[1].split()[0]))
hits.append(int(fields[3].split()[0]))
print(stand)
print(hits)
# prints
# [73.0, 61.0, 73.0, 70.0, 72.0]
# [16, 28, 16, 19, 17]