示例:
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
(带有%的数字)
尝试在论坛中查找并使用文件和内容中的阅读行,但我是初学者,因此无法正常工作,所以 在python中,请先感谢
答案 0 :(得分:2)
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
答案 1 :(得分:1)
您可以使用regular expressions:
import re
with open("file.txt") as f:
for line in f:
r = re.search(r'(\d+(\.[\d]+)?)%', line)
if r:
print(r.group(1))
根据您的示例数据,得出:
78.57
77.59
76.00
答案 2 :(得分:-1)
假设您已将所有输入内容读入列表。您可以尝试一下。
inp = ['1', 'Yuumi', '78.59%', ...]
out = [float(a.remove('%')) for a in inp if '%' in a]
如果愿意,您可以将文件中的所有输入内容放入列表中
with open('my_file.txt') as file:
inp = file.read.splitlines()