python - 查找并替换引号之间的字符串

标签 python regex file-io

我正在读取一个文件,我想替换出现在两个双引号之间的任何文本,如下所示:

如果文件输入是:

Hi, I'm an example file! "Hi there example file."

"I'm mostly just here to get this quote to be colored!"

输出应该是:

Hi, I'm an example file! [color=x]"Hi there example file."[/color]

[color=x]"I'm mostly just here to get this quote to be colored!"[/color]


我编写了这三个模块来做到这一点,前两个模块可以工作,但最后一个则不行。

模块 1:

__author__ = 'Joker'
import os
import sys
import re
import fileinput

print ("Text to search for:")
textToSearch = ( '" ' )

print ("Text to replace it with:")
textToReplace = ( '"[/color] ' )

print ("File to perform Search-Replace on:")
fileToSearch  = ( "D:\Coding projects\post edit\post.txt" )


tempFile = open( fileToSearch, 'r+' )

for line in fileinput.input( fileToSearch ):
    if textToSearch in line :
        print('Match Found')
    else:
        print('Match Not Found!!')
    tempFile.write( line.replace( textToSearch, textToReplace ) )
tempFile.close()

input( '\n\n Press Enter to exit...' )

模块 2:

__author__ = 'Joker'
import os
import sys
import re
import fileinput

print ("Text to search for:")
textToSearch = ( ' "' )

print ("Text to replace it with:")
textToReplace = ( ' [color=#66ccff]"' )

print ("File to perform Search-Replace on:")
fileToSearch  = ( "D:\Coding projects\post edit\post.txt" )

tempFile = open( fileToSearch, 'r+' )

for line in fileinput.input( fileToSearch ):
    if textToSearch in line :
        print('Match Found')
    else:
        print('Match Not Found!!')
    tempFile.write( line.replace( textToSearch, textToReplace ) )
tempFile.close()

input( '\n\n Press Enter to exit...' )

模块 3:

__author__ = 'Joker'
import os
import sys
import re
import fileinput

print ("Text to search for:")
textToSearch = ( r'\n"')

print ("Text to replace it with:")
textToReplace = (r'\n[color=#66ccff]"' )

print ("File to perform Search-Replace on:")
fileToSearch  = ( "D:\Coding projects\post edit\post.txt" )

tempFile = open( fileToSearch, 'r+' )

for line in fileinput.input( fileToSearch ):
    if textToSearch in line :
        print('Match Found')
    else:
        print('Match Not Found!!')
    tempFile.write( line.replace( textToSearch, textToReplace ) )
tempFile.close()

input( '\n\n Press Enter to exit...' )

奖励:有没有办法将这三个模块的功能合并为一个?

最佳答案

尝试使用re 正则表达式模块:

import re 

text = open(filename).read()  # read the entire file content

# define your tags here
opening_tag = '[color=x]'
closing_tag = '[/color]'

matches = re.findall(r'\"(.+?)\"',text)  # match text between two quotes
for m in matches:
  text = text.replace('\"%s\"' % m, '%s\"%s\"%s' % (opening_tag, m, closing_tag))  # override text to include tags

# write modified text with tags to file
with open(filename, 'w') as f:
  f.write(text)

#  for the input you specified, the new file should be:
>> [color=x]"Hi there example file."[/color]
>> [color=x]"I'm mostly just here to get this quote to be colored!"[/color]

关于python - 查找并替换引号之间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811624/

相关文章:

python - 将日期和时间戳格式的数据插入 PostgreSQL

python - 如何有效地操作子数组,例如计算行列式、逆矩阵、

python - 你如何删除多行python字符串中的空格?

javascript - 使用正则表达式删除点也会删除 *

javascript - 仅当某些字符跟在字符串后时才匹配正则表达式

vb.net - 如何将“浏览到文件”对话框添加到VB.NET应用程序

c# - System.IO.FileShare 有限制吗?

python - 在 python 中给定开始和结束位置的用户之间读取文本文件

python - 查找用于连接到我的套接字服务器的主机名

Jquery 和 Regex 数字检查