2016-08-17 49 views
0

我的問題在於我有大量的文件。每個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 
+0

請幫助我今天需要完成它:(:(:( – MaciejPL

回答

1

read()each_targ但你沒有任何地方存儲字符串。

相反,您將文件句柄each_targ改爲.sub並導致類型不匹配。你可以只說:

findtargetID = re.sub('id="\w{3}\d{13}"',str(IDtoreplace), each_targ.read()) 
+0

是的,它不叫錯誤,但不保存文件我的意思是一切如故 – MaciejPL

+0

我什麼。做錯了該文件不想保存 – MaciejPL

+0

我做了這樣的事情,但它增加了文本,而不是編輯 targetID =(re.sub(r'id =「\ w {3} \ d {13}」',strtarget, each_targ.read()) each_targ.write(targetID) each_targ.close() – MaciejPL