简体   繁体   English

如何在python中替换特定单词下的值?

[英]how to replace a value under specific word in python?

I have this code to interpolate between the values and return Z value for specific X and Y. I am trying to open another file ( world.txt ) and paste the value under specific name Nz . 我有这个代码在值之间插值并返​​回特定X和Y的Z值。我正在尝试打开另一个文件( world.txt )并将值粘贴到特定名称Nz

The way the file is organised is 文件的组织方式是

Nx
5
Ny
0.1
Nz
0.8

But what i am getting is givemeZ(0.5,0.04) instead of the value 0.78 但我得到的是givemeZ(0.5,0.04)而不是0.78

Would you please suggest some ideas of how to fix it? 您能否提出一些如何解决问题的建议?

import numpy as np
from scipy import interpolate
from scipy.interpolate import griddata
import itertools
from scipy.optimize import curve_fit

xx= 0.15, 0.3, 0.45, 0.5, 0.55
yy= 0.01, 0.02, 0.03, 0.04, 0.05
zz= 0.75, 0.76, 0.77, 0.78, 0.79

tck = interpolate.bisplrep(xx, yy, zz, s=0)    
def givemeZ(X,Y):
    return interpolate.bisplev(X,Y,tck)
## to replace the value 
with open('world.txt', 'r') as file :
  filedata = file.read()
# Replace the target string
filedata = filedata.replace('Nz', 'givemeZ(0.5,0.01)')
# Write the file out again
with open('world.txt', 'w') as file:
  file.write(filedata)

Well, yeah. 嗯,是的 You pass filedata.replace the string literal 'givemeZ(0.5,0.01)' . 你传递了filedata.replace 字符串文字 'givemeZ(0.5,0.01)' You should pass it the actual value, converted to a string, like 您应该将实际值传递给它,转换为字符串,就像

filedata = filedata.replace('Nz', str(givemeZ(0.5,0.01)))

Depending on the details, you may be able to get away without the str() call - I'm not sure if replace will smoothly handle non- str arguments that can be converted to str . 根据细节,您可以在没有str()调用的情况下离开 - 我不确定replace是否可以平滑地处理可以转换为strstr参数。 Doing the conversion yourself explicitly should work though. 明确地自己进行转换应该可行。


To replace the next line, you need some logic that runs in a loop - I don't think you'll be able to do it purely by a replace . 要替换下一行,您需要一些在循环中运行的逻辑 - 我认为您不能仅仅通过replace来完成它。 You could do something like this: 你可以这样做:

new_lines = []
previous_line = ''
with open('world.txt', mode = 'r') as f:
    for current_line in f:
        if previous_line == 'Nz':
            new_lines.append(str(givemeZ(0.5,0.01)))
        else:
            new_lines.append(current_line)
        previous_line = current_line

new_text = '\n'.join(new_lines)

You could then write the new text back to world.txt - note that the operations you're doing here, even in your original code, do not modify the actual file, just the Python string in memory. 然后,您可以将新文本写回world.txt - 请注意,您在此处执行的操作(即使在原始代码中)也不会修改实际文件,只修改内存中的Python字符串。

Your code works as expected, the line 您的代码按预期工作,即行

filedata = filedata.replace('Nz', 'givemeZ(0.5,0.01)')

replaces the string Nz with the string givemeZ(0.5,0.01) . 用字符串givemeZ(0.5,0.01)替换字符串Nz

You want to replaze Nz with the return value of givemeZ(0.5,0.01) . 您想要使用givemeZ的返回值givemeZ(0.5,0.01)重放Nz Use the following: 使用以下内容:

filedata = filedata.replace('Nz', str(givemeZ(0.5, 0.01)))

This takes the return value of givemeZ() and converts this value to a string. 这将获取givemeZ()的返回值并将此值转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM