【发布时间】:2020-06-18 02:27:23
【问题描述】:
我想在几个文本文件中查找浮点数并将其替换为整数。
我要转换的每个文本文件都有一个浮点值。它总是在特定关键字之后,并且必须乘以 10.000。
例如浮点数 1.5 应该变成整数 15.000
不过 1.5 之后的其他浮点数不必更改
def edit(file):
with open(file, 'r') as f:
filedata = f.read()
for line in filedata:
if "keyword" in line:
filedata = filedata.replace(re.search(r"\d+\.\d+", line).group(), str(10000*re.search(r"\d+\.\d+", line).group()))
with open(file, 'w') as f:
f.write(filedata)
我试图用正则表达式替换浮点数。但这不起作用
示例文件摘录
abcdef 178 211 208 220
ghijkl 0 0 0 0
keyword 1.50 1.63 1.56 1.45
【问题讨论】:
-
你能告诉我们输入文件的例子吗?
-
是的,我会修改问题
标签: python text type-conversion integer