我有一个这种类型的 txt 文件:
1,23,4,5
4.6,5,7,8.9
2,3,45,21
2,4.2,5,6
58,a,b,c,d
d,e,f,g,h
我只想提取第一个和最后一个数值。在我的下一个程序中,我能够从字符串中删除字符并获得一个带有数字的字符串:
import re
with open("C:\testo.txt", "r") as fp:
lines=fp.readlines()
for i in range(0, len(lines)):
x=lines[i]
result=re.match('\d+', x)
if result != None:
valori=result.group()
print(valori)
我的输出是:
1
23
4
5
4.6
5
7
8.9
2
3
45
21
2
4.2
5
6
58
现在我想要的输出是:
1
58