繁体   English   中英

替换txt文件中字符串中的指定字符

[英]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}”,但是它不起作用。

对于这些事情,正则表达式中包含特殊字符。 简要总结如下:

  • 如果您想说“ 1个或多个,但至少1个”,请使用+
  • 当您想说“ 0或任何数字,但可能没有”时,请使用*
  • 当您想说“无,但如果存在,仅重复一次”时,使用?

您可以在要使用“重复”修饰符进行修饰的表达式之后使用它。

有关更多信息,请通读文档

现在您的要求是“有字符,长度不固定 ,基于此,我们必须使用+

尝试使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM