2017-09-15 69 views
0

我想做一個替換腳本。在Python中替換序列

它應該將str1替換爲str2。 我的文件具有基於xml的結構。 例如,我有:

...word1'#13#10'word2'#13#10'word3... = ...word1'#13#10'word3... 

我想刪除字符串的某些部分。 我用這個在腳本:

Lines[i] = Lines[i].replace(key, DataBase[key]) 

我已經檢查了「鑰匙」和「數據庫[關鍵]」正確定義。如果我用「print()」將它們打印到控制檯中 - 它看起來就像它必須。 但是,然後腳本執行它不會像這樣改變序列 - 與'#13#10'。沒有任何特定符號的密鑰對可以正常工作。 我能做什麼?爲什麼它不能很好地工作? 完整的腳本:

import configparser 
#import time 

config = configparser.ConfigParser() # init configparser 
config.optionxform = str 
config.read("SocratToCortesExpress.cfg") # config file 

print("Config file - readed") 

filePath = config.get("PATH", "old_file") # config file - with names of files, pairs of words 

DataStrings = config.items("DATA") # read pairs 
DataBase = dict() # initialization of dictionary 
print("Dictionary - initialized") 

for Dstr in DataStrings: # old and new words for a replacement 
    SocratName = Dstr[0] 
    CortesName = Dstr[1]  
    DataBase[SocratName] = CortesName 


print("Dictionary - fulfilled") 

with open(filePath, "r", encoding='utf-8-sig') as ResultFile: # input file Lines = ResultFile.readlines() 

print("Old file - uploaded") 

f1 = open('logkeys.txt', 'w') 
for key in DataBase.keys(): 
    try: 
     f1.write('\n'+key+'\n'+DataBase[key]+'\n') 
    except Exception as e: #errors 
      f2 = open('log.txt', 'w') 
      f2.write('An exceptional thing happed - %s' %e) 
      f2.close() 
f1.close() 


for i in range(len(Lines)): # brutforce - all over input file 
    #Lines[i] = Lines[i].replace('\ufeff', '') #some weird symbol 
    for key in DataBase.keys():  
     try: 
      Lines[i] = Lines[i].replace(key, DataBase[key]) #replacing 
     except Exception as e: #errors 
      f2 = open('log.txt', 'w')   
      f2.write('An exceptional thing happed - %s' %e) 
      f2.close() 


print("Sequences - replaced") 

outFileName = config.get("PATH", "new_file") # define output file 

print("Exit file - initialized") 

with open(outFileName, "a", encoding='utf-8-sig') as outFile: # save 
    for line in Lines:  
     outFile.write(line) 

print("OK") 
+1

也許你沒有顯示的腳本部分有問題;由於顯而易見的原因無法確定。 –

+2

請添加您的代碼,以便我們幫助您。 – Fejs

+0

看看迄今爲止顯示的內容,似乎沒有錯誤。問題可能出現在你沒有顯示的部分。也許如果你發佈了更多的代碼,我們可以幫助更好。 –

回答

2

你試過嗎?

>>> s = "word1'#13#10'word2'#13#10'word3" 
>>> s.replace("'word2'#13#10'", '') 
"word1'#13#10word3" 
+0

所以,我試了一下。但是腳本,而不是shell。不,不起作用。也許是因爲我的word2在西裏爾字母 –

+0

如果這是一個腳本或交互式運行,它無關。對於UTF-8更換,請查看https://stackoverflow.com/questions/13093727/how-to-replace-unicode-characters-in-string-with-something-else-python –

+0

非常感謝。也許我現在正走在正確的道路上。我已經在使用UTF-8編碼:open(filePath,「r」,encoding ='utf-8-sig')。它幫助我正確讀取文件,包括BOM。但是,我仍然無法取代包含'#13#10'的字符串。我不知道,也許有方法來查找和替換字節序列,而不是字符串?首先編碼我的字符串,進行替換,然後再編碼爲UTF-8。但我不確定是否存在字節替換。 –