Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在对字符串执行数学运算后查找和替换_Python_Python 3.x_Replace_Parameters_Find - Fatal编程技术网

Python 如何在对字符串执行数学运算后查找和替换

Python 如何在对字符串执行数学运算后查找和替换,python,python-3.x,replace,parameters,find,Python,Python 3.x,Replace,Parameters,Find,我有一个包含以下字符串的文件 input complex_data_BITWIDTH; output complex_data_(2*BITWIDTH+1); 假设比特宽度=8 我想要以下输出 input complex_data_8; output complex_data_17; 如何在python中使用find和replace来实现这一点。我建议查看reRegEx库以进行字符串替换和字符串搜索,以及eval()函数以对字符串执行数学操作 示例(假设要计算的内容周围始终有括号): 如果知

我有一个包含以下字符串的文件

input complex_data_BITWIDTH;
output complex_data_(2*BITWIDTH+1);
假设比特宽度=8

我想要以下输出

input complex_data_8;
output complex_data_17;

如何在python中使用find和replace来实现这一点。我建议查看
re
RegEx库以进行字符串替换和字符串搜索,以及
eval()
函数以对字符串执行数学操作

示例(假设要计算的内容周围始终有括号):


如果知道要更改的值,可以使用变量,一个用于搜索值,另一个用于搜索新值

BITWIDTH=8
新的\u位宽度=2*位宽度+1
string_input='complex_data_8;'
string\u output=string\u input.replace(str(BITWIDTH),str(NEW\u BITWIDTH))
如果您不知道该值,那么您需要先获取它,然后使用它进行操作

string_input='complex_data_8;'
bitwidth=string_输入。拆分(“”“”)[-1]。替换(“;”,“”)
新的\u位宽度=2*int(位宽度)+1
string\u output=string\u input.replace(位宽,str(新位宽))

试试这个,我不知道有没有捷径,但我想它行得通

A="complex_data_13"
no1='0'
st1=''
for x in A:
    if x.isdigit()==True:
        no1+=x
    else:
        st1+=x
calc =2*int(no1)+1
print(st1+str(calc))

每当
()
中有内容时,是否需要执行算术?当需要执行数学计算时,是否保证括号
()
在那里?文件变量中是否所有大写字符串都需要替换?我想计算
中的表达式()
这是位宽度的函数。谢谢!如何确保在
()
中计算表达式,该表达式中只包含位宽度。我不想评估abc(def*ghi)
A="complex_data_13"
no1='0'
st1=''
for x in A:
    if x.isdigit()==True:
        no1+=x
    else:
        st1+=x
calc =2*int(no1)+1
print(st1+str(calc))