![](/img/trans.png)
[英]Replacing string in txt file with content of another txt file (regular expressions)
[英]replacing appointed characters in a string in txt file
大家好...我想從包含以下內容的文本文件中提取文本“ DesingerXXX”:
C DesignerTEE edBore 1 1/42006
Cylinder SingleVerticalB DesignerHHJ e 1 1/8Cooling 1
EngineBore 11/16 DesignerTDT 8Length 3Width 3
EngineCy DesignerHEE Inline2008Bore 1
Height 4TheChallen DesignerTET e 1Stroke 1P 305
Height 8C 606Wall15ccG DesignerQBG ccGasEngineJ 142
Height DesignerEQE C 60150ccGas2007
Anidea將使用“設計器”作為關鍵,將每行分為關鍵部分和關鍵部分之后的兩部分。
file_object = open('C:\\file.txt')
lines = file_object.readlines()
for line in lines:
if 'Designer' in line:
where = line.find('Designer')
before = line[0:where]
after = line[where:len(line)]
file_object.close()
在“鍵之前”部分,我需要找到LAST空格(''),並替換為另一個符號/字符。
在“鍵之后”部分,我需要找到第一個空格(''),並替換為另一個符號/字符。
然后,我可以將其切成薄片,然后根據新的符號/字符來選擇所需的對象。
有沒有更好的方法來獲取想要的文本? 是否可以替換指定的密鑰空間?
在字符串替換功能中,我可以限制替換的時間,但不能限制我可以替換的時間。 我怎樣才能做到這一點?
謝謝
使用正則表達式,這是一項瑣碎的任務:
>>> s = '''C DesignerTEE edBore 1 1/42006
... Cylinder SingleVerticalB DesignerHHJ e 1 1/8Cooling 1
... EngineBore 11/16 DesignerTDT 8Length 3Width 3
... EngineCy DesignerHEE Inline2008Bore 1
... Height 4TheChallen DesignerTET e 1Stroke 1P 305
... Height 8C 606Wall15ccG DesignerQBG ccGasEngineJ 142
... Height DesignerEQE C 60150ccGas2007'''
>>> import re
>>> exp = 'Designer[A-Z]{3}'
>>> re.findall(exp, s)
['DesignerTEE', 'DesignerHHJ', 'DesignerTDT', 'DesignerHEE', 'DesignerTET', 'DesignerQBG', 'DesignerEQE']
正則表達式為Designer[AZ]{3}
,這表示字母Designer
,后跟大寫字母A
到大寫字母Z
的任何字母,出現3次,並且只有3次。
因此,它將不匹配DesignerABCD
(4個字母),也將不匹配Desginer123
(123是無效字母)。
它也不會與Designerabc
匹配(abc是小寫字母)。 要忽略這種情況,可以傳遞一個可選標志re.I
作為第三個參數; 但這也將與designerabc
匹配(對於正則表達式,您必須非常具體)。
因此,要使其與Designer
匹配,並緊跟3個大寫或小寫字母,您必須將表達式更改為Designer[Aa-zZ]{3}
。
如果要搜索和替換,則可以使用re.sub
替換匹配項。 因此,如果我想將所有匹配項替換為單詞“ hello”:
>>> x = re.sub(exp, 'hello', s)
>>> print(x)
C hello edBore 1 1/42006
Cylinder SingleVerticalB hello e 1 1/8Cooling 1
EngineBore 11/16 hello 8Length 3Width 3
EngineCy hello Inline2008Bore 1
Height 4TheChallen hello e 1Stroke 1P 305
Height 8C 606Wall15ccG hello ccGasEngineJ 142
Height hello C 60150ccGas2007
如果在“ Designer”之前和之后都有字符,並且字符長度不固定怎么辦? 我嘗試了[[Aa-zZ] Designer [Aa-zZ] {0〜9}”,但是它不起作用。
對於這些事情,正則表達式中包含特殊字符。 簡要總結如下:
+
*
?
您可以在要使用“重復”修飾符進行修飾的表達式之后使用它。
有關更多信息,請通讀文檔 。
現在您的要求是“有字符,但長度不固定 ” ,基於此,我們必須使用+
。
嘗試使用re.sub
。 正則表達式與您的關鍵字匹配,並用空格包圍。 sub的第二個參數,用your_special_char替換環繞空格(在我的腳本中為連字符)
>>> import re
>>> with open('file.txt') as file_object:
... your_special_char = '-'
... for line in file_object:
... formated_line = re.sub(r'(\s)(Designer[A-Z]{3})(\s)', r'%s\2%s' % (your_special_char,your_special_char), line)
... print formated_line
...
C -DesignerTEE-edBore 1 1/42006
Cylinder SingleVerticalB-DesignerHHJ-e 1 1/8Cooling 1
EngineBore 11/16-DesignerTDT-8Length 3Width 3
EngineCy-DesignerHEE-Inline2008Bore 1
Height 4TheChallen-DesignerTET-e 1Stroke 1P 305
Height 8C 606Wall15ccG-DesignerQBG-ccGasEngineJ 142
Height-DesignerEQE-C 60150ccGas2007
Maroun Maroun提到了“為什么不簡單地分割字符串”。 所以猜測一種可行的方法是:
import re
file_object = open('C:\\file.txt')
lines = file_object.readlines()
b = []
for line in lines:
a = line.split()
for aa in a:
b.append(aa)
for bb in b:
if 'Designer' in bb:
print bb
file_object.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.