我的問題在於我有大量的文件。每個xml文件都包含ID。我有一組源文件和目標文件。源文件具有名稱A和ID = B。目標文件具有名稱B和ID = B 我需要做的是將源ID B與目標名稱B匹配,然後用源名稱A替換目標ID = B。希望其清除Python:用正則表達式替換字符串
這裏是我的代碼
import os
import re
sourcepath = input('Path to source folder:\n')
targetpath = input('Path to target folder:\n')
for root,dir,source in os.walk(sourcepath):
for each_file in source:
os.chdir(root)
correctID = each_file[:16]
each_xml = open(each_file, 'r', encoding='utf8').read()
findsourceID = re.findall('id="\w{3}\d{13}"', each_xml)
StringID = str(findsourceID)
correctFilename = StringID[6:22]
IDtoreplace = 'id="' + correctID + '"'
print(IDtoreplace)
for main,folder,target in os.walk(targetpath):
for each_target in target:
os.chdir(main)
targetname = each_target[:16]
if targetname == correctFilename:
with open(each_target, 'r+', encoding='utf8') as each_targ:
each_targ.read()
findtargetID = re.sub('id="\w{3}\d{13}"',str(IDtoreplace), each_targ)
each_targ.close()
這裏是錯誤
File "C:/Users/ms/Desktop/Project/test.py", line 23, in <module>
findtargetID = re.sub('id="\w{3}\d{13}"',str(IDtoreplace), each_targ)
File "C:\Users\ms\AppData\Local\Programs\Python\Python35\lib\re.py", line 182, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
請幫助我今天需要完成它:(:(:( – MaciejPL