Below is my code -
以下是我的代码 -
import tarfile
import os
import sys
import re
script, bak = sys.argv
bakfile = str(bak)
currentwd = os.path.dirname(os.path.realpath(__file__))
file_to_work = tarfile.open(name=currentwd+"/"+bakfile, mode="r")
file_to_work.extractall()
currentwd = os.path.dirname(os.path.realpath(__file__))
with open(currentwd+"/onedb.xml", "r") as file:
f = file.read()
words = re.findall(r'{ssha}_\w*?=', f)
re.sub(words,r'string_to_replace',f)
I used tarfile
module and extracted a gzfile, from the extracted files, picked onedb.xml. Used regex to find the strings and that was successful.
我使用了tarfile模块并从解压缩的文件中提取了一个gzfile,并选择了onedb.xml。使用正则表达式来查找字符串并且成功。
Now when I try to replace searched strings using re.sub
, I get the below error.
现在当我尝试使用re.sub替换搜索的字符串时,我得到以下错误。
Traceback (most recent call last):
File "preset.py", line 16, in <module>
re.sub(words,r'string_to_replace',f)
File "/usr/lib/python2.7/re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "/usr/lib/python2.7/re.py", line 232, in _compile
p = _cache.get(cachekey)
TypeError: unhashable type: 'list'
1 个解决方案
#1
0
Use all in one Expression:
在一个表达式中使用all:
re.sub(r'{ssha}_\w*?=', r'string_to_replace', f)
#1
0
Use all in one Expression:
在一个表达式中使用all:
re.sub(r'{ssha}_\w*?=', r'string_to_replace', f)