2016-03-05 79 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)