2016-03-05 144 views
2

我读一个文件,我想换成两个双引号这样之间的任何文本字符串之间:查找和替换报价

如果文件输入:

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...') 

加成:是有办法这三个模块的功能结合变成一个?使用reregular expression模块为

+0

我已经在那里输入了。增加了输出。 – TheJoker

回答

1

尝试:

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] 
+0

追溯(最近呼叫最后一个): 第15行,在 f.write() TypeError:函数只需要1个参数(0给定) 给我一个错误,挠我的头脑搞清楚。 – TheJoker

+0

更新回答@TheJoker。 – Forge

+0

错误已修复,但是在运行代码之后,没有任何对话正在我的文件内被新格式替换。 **当前代码:** [链接](http://puu.sh/nvtRK/577907ca59.png)**运行后的post.txt:** [链接](http://puu.sh/nvtU1/1259f361d4 .png) - 文件发生了变化(因为notepad ++要求重新加载它。)但是没有一个内容被改变。 – TheJoker

0

结束了使用@锻造的回答为灵感。所以他的主要信用。但是,这是我最终修复它的方式。包括最终结果:

import re 

string = open('post.txt').read() # read the entire file content 
cfile = open('character.txt').read() #Read character 

#select a color 
variable=raw_input('which character are you using?\n') 
print variable 
ini=cfile.find(variable)+(len(variable)+1) 
rest=cfile[ini:] 
search_enter=rest.find('\n') 
color=str(rest[:search_enter]) 

# define your tags here 
opening_tag = '[color=%s]' % (color) 
closing_tag = '[/color]' 

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

print string 

# write tagged text to file 
with open('post.txt', 'w') as f: 
    f.write(string)