问题描述
我正在使用python编写一个简单的程序,该程序将示例c ++“ .cpp”文件作为字符串,然后查找声明到其中的所有字符串。然后,我想将该字符串替换为其等效的十六进制代码,例如“ H”等于“ \ x48”。
我的代码是
f = open("sample.cpp","r")
f1 = f.read()
regex = r"\"(?:(?:(?!(?<!\\)\").)*)\""
ii=0
for str2021 in find:
print("Output Of Encode=")
str2021="".join(r'\x{0:x}'.format(ord(c)) for c in find[ii])
print (str2021)
ii=ii+1
subst='\x22\x48\x22'
result = re.sub(regex,subst,f1,0)
if result:
print("substituted op=")
print (result)
现在,当我打印结果值时,它显示为“ H”而不是“ \ x22 \ x48 \ x22”。如何在python 3.8中强制执行此操作?
如果我这样做的话
result = re.sub(regex,str2021,0)
它给出了一个错误
raise s.error('bad escape %s' % this,len(this))
re.error: bad escape \x at position 0
我想对其进行迭代,以便在cpp文件中使用正则表达式找到的每个字符串中,代码都将自动将字符串转换为其等效的Unicode十六进制代码,
Sample.cpp
string a="abc";
string b="H";
它应该像这样更改此cpp文件
string a="\x61\x62\x63";
string b="\x48";
请提出解决方案
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)