首页 > 解决方案 > 删除除带有 % 的行之外的所有内容并删除 %

问题描述

例子:

1
Yuumi
78.57%
2075
1956
0.64
62.8
Thresh
77.59%
1079
917
0.83
19.3
Braum
76.00%
1868
1315
1.44
38.0

我希望它是:

78.57
77.59
76.00

(带 % 的数字)

尝试在论坛中查找并使用文件和内容中的 readlines 但我是初学者并且无法使其在 python 中正常工作,在此先感谢

标签: pythonpython-3.xtext

解决方案


with open('path/to/the/file') as fd:
    for line in fd:
        if '%' in line:
            print(line.strip().replace('%', ''))

或者您可以在 shell 中执行此操作:

grep "%" filename | sed 's/%//g' >newfile

推荐阅读