Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,如何查找和替换每行特定字符后的文本?_Python_Regex_Csv_Case Insensitive - Fatal编程技术网

在Python中,如何查找和替换每行特定字符后的文本?

在Python中,如何查找和替换每行特定字符后的文本?,python,regex,csv,case-insensitive,Python,Regex,Csv,Case Insensitive,我有多个文件,行如下,需要循环查找并替换“文本”,但仅在“=”之后 static.TEXT.here=change.TEXT.here 这是我当前的代码,但我只是在“=”之后指定,因为需要替换的“文本”在所有文件中的位置不一致 import re src = open(r"sourcefile.txt").read() dest = open(r"destinationfile.txt","w") dest.write( re.sub(currentText,replacementText,sr

我有多个文件,行如下,需要循环查找并替换“文本”,但仅在“=”之后

static.TEXT.here=change.TEXT.here

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

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]

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

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的文件,以便测试该过程
2.浏览文件并打印每一行,练习用python读取文件
3.在
=
4.我们希望获得
=
端的第二部分,因此我们将对其进行切片以执行
索引[1]
索引[-1]
5.将
文本
替换为
**MYTEXT**
6.现在我们有了正确的
=
的后半部分,让我们重新添加第一部分 7.最后,我们将其写入一个新文件 8.确认它看起来是正确的
上述内容是否可以简化并写得更好?当然使用regex可以用更少的步骤完成上述操作吗?对但我已经包含了上面的步骤,希望能一步一步地引导您了解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还不太熟悉,所以我肯定这是我忽略的一点,非常感谢。我使用regex来表示不区分大小写的标志,但这让我对python中的流程有了更好的理解,而且碰巧只有两种变体(大写和非大写),所以我可以添加另一个
$ 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
>>> 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
>>> 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']
>>> for line in open('sourcefile.txt'):
...     print line.split('=')[-1]
... 
change.TEXT.here

change.TEXT.here.more

change.TEXT.here.even.more
>>> 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
>>> 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
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)
$ 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