我想在几个文本文件中查找并用整数替换浮点值
我要转换的每个文本文件有一个浮点值。它总是在一个特定的关键字之后,必须乘以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)
我试图用正则表达式来替换float。但这不起作用
示例文件摘录
abcdef 178 211 208 220
ghijkl 0 0 0 0
keyword 1.50 1.63 1.56 1.45
您可以使用lines = filedata.split("n")
对行进行迭代。请小心,因为filedata
是一个包含整个文件的大字符串。当您执行for line in filedata
时,您对文件的每个字符都进行了迭代。。。
我还使用了另一种方法(没有regex
(来查找数字并更改它们。
def edit(file):
with open(file, "r") as f:
filedata = f.read()
lines = filedata.split("n") # list of lines
for index, line in enumerate(lines):
if "keyword" in line:
words = line.split() # ['keyword', '1.50', '1.63', '1.56', '1.45']
for i, w in enumerate(words):
try:
# transform number to float, multiply by 10000
# then transform to integer, then back to string
new_word = str(int(float(w)*10000))
words[i] = new_word
except:
pass
lines[index] = " ".join(words)
new_data = "n".join(lines) # store new data to overwrite file
with open(file, "w") as f: # open file with write permission
f.write(new_data) # overwrite the file with our modified data
edit("myfile.txt")
输出:
# myfile.txt
abcdef 178 211 208 220
ghijkl 0 0 0 0
keyword 15000 16299 15600 14500
编辑:更紧凑的方式
def edit(file):
with open(file, "r") as f:
filedata = f.read()
line = [x for x in filedata.split("n") if "keyword" in x][0]
new_line = line
for word in line.split():
try: new_line = new_line.replace(word, str(int(float(word)*10000)))
except: pass
with open(file, "w") as f: # open file with write permission
f.write(filedata.replace(line, new_line)) # overwrite the file with our modified data
edit("myfile.txt")
当您发现自己在循环中使用正则表达式时,应该在循环外编译它。
接下来,如果要替换一行中的值,则不应在整个文件中搜索该值。
最后,您必须将字符串强制转换为数字类型才能对其进行操作。如果不这样做,您将只重复字符串('10' * 2
是'1010'
,而不是20
或'20'
(
以下是您的代码的一个可能改进:
def edit(file):
with open(file, 'r') as f:
rx = re.compile(r"d+.d+") # compile the regex only once
filedata = f.readlines() # get a list of the lines of the file
for i, line in enumerate(filedata): # and enumerate them
if "keyword" in line:
val = re.search(r"d+.d+", line).group() # split the complex line
newval = str(int(float(val) * 10000))
filedata[i] = line.replace(val, newval) # replace only the current line
break # no need to proceed further
with open(file, 'w') as f:
f.write(filedata)