【问题标题】:How can I find and replace text after a specific character on each line in Python?如何在 Python 中的每一行上查找和替换特定字符之后的文本?
【发布时间】:2017-04-29 05:43:15
【问题描述】:

我有多个文件,如下所示,需要循环查找并替换“TEXT”,但只能在“=”之后

static.TEXT.here=change.TEXT.here

这是我当前的代码,但我只能在“=”之后指定,因为需要替换的“TEXT”在所有文件中的位置不一致

import re
src = open(r"sourcefile.txt").read()
dest = open(r"destinationfile.txt","w")
dest.write( re.sub(currentText,replacementText,src, flags=re.I) )
dest.close()

编辑

我采用了稍微不同的方法并导入了 csv 并使用 '=' 作为分隔符来创建单独的行,但现在我很难迭代我现有的 re.sub 代码来查找和替换文本,我使用的代码相关行:

import csv
with open("sourcefile.txt", 'rb') as csvfile:
csvreader = csv.reader(csvfile, delimiter= '=')
for row in csvreader:
    if len(row) >1:
        print row[1]

【问题讨论】:

    标签: python regex csv case-insensitive


    【解决方案1】:

    您可以遍历文件中的每一行并在= 之后进行替换。例如:

    formatted_contents = ''
    for line in open(r"sourcefile.txt"):
        line_formatted = line.split('=')[-1].replace('TEXT', '**my_text**')
        formatted_contents += line_formatted
    

    这假设每一行都有一个=。如果不是在每一行中,您可能需要为您想要执行的操作添加一些条件。

    更新

    让我们一步一步来。

    1。创建名为sourcefile.txt 的文件,以便我可以测试该过程

    $ cat sourcefile.txt 
    static.TEXT.here=change.TEXT.here
    more.static.TEXT.here=change.TEXT.here.more
    even.more.static.TEXT.here=change.TEXT.here.even.more
    

    2。浏览文件并打印每一行以练习在python中读取文件

    >>> for line in open('sourcefile.txt'):
    ...     print line
    ... 
    static.TEXT.here=change.TEXT.here
    
    more.static.TEXT.here=change.TEXT.here.more
    
    even.more.static.TEXT.here=change.TEXT.here.even.more
    

    3。在= 上拆分文件的内容

    >>> for line in open('sourcefile.txt'):
    ...     print line.split('=')
    ... 
    ['static.TEXT.here', 'change.TEXT.here\n']
    ['more.static.TEXT.here', 'change.TEXT.here.more\n']
    ['even.more.static.TEXT.here', 'change.TEXT.here.even.more\n']
    

    4。我们想取= 边的第二部分,所以我们将它切片做index[1]index[-1]

    >>> for line in open('sourcefile.txt'):
    ...     print line.split('=')[-1]
    ... 
    change.TEXT.here
    
    change.TEXT.here.more
    
    change.TEXT.here.even.more
    

    5。将TEXT 替换为**MYTEXT**

    >>> for line in open('sourcefile.txt'):
    ...     print line.split('=')[-1].replace('TEXT','**MYTEXT**')
    ... 
    change.**MYTEXT**.here
    
    change.**MYTEXT**.here.more
    
    change.**MYTEXT**.here.even.more
    

    6。现在我们有了正确的 = 的后半部分,让我们将第一部分添加回

    >>> for line in open('sourcefile.txt'):
    ...     print line.split('=')[0] + '=' + line.split('=')[-1].replace('TEXT','**MYTEXT**')
    ... 
    static.TEXT.here=change.**MYTEXT**.here
    
    more.static.TEXT.here=change.**MYTEXT**.here.more
    
    even.more.static.TEXT.here=change.**MYTEXT**.here.even.more
    

    7。最后,我们将其写入一个新文件

    newfile=open('destinationfile.txt','w')
    for line in open('sourcefile.txt'):
    txt = line.split('=')[0] + '=' + line.split('=')[-1].replace('TEXT','**MYTEXT**')
        print txt
        newfile.write(txt)
    

    8。确认它看起来正确

    $ cat destinationfile.txt 
    static.TEXT.here=change.**MYTEXT**.here
    more.static.TEXT.here=change.**MYTEXT**.here.more
    even.more.static.TEXT.here=change.**MYTEXT**.here.even.more
    

    上面的内容可以简化并写得更好吗?当然。使用正则表达式可以用更少的步骤完成上述操作吗?是的。但我已经包含了上述步骤,希望能一步一步地引导你了解 python 发生了什么。希望对您有所帮助。

    【讨论】:

    • 为快速回复干杯,我已尝试实现提供的示例,但没有收到任何错误,但似乎也没有对文件进行更改。用于参考的确切代码: formatted_contents = '' for line in open(r"C:\Temp\test\test.txt"): line_formatted = line.split('=')[-1].replace('TEXT' , 'MyText' ) formatted_contents += line_formatted 我对 Python 还很陌生,所以我确定这是我忽略的东西
    • 非常感谢。我将正则表达式用于不区分大小写的标志,但这让我对 python 中的过程有了更好的理解,而且碰巧只有两种变体(大写和非大写),所以我可以添加另一个 .replace
    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 2021-02-25
    • 2022-01-11
    • 2020-03-06
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多