1

换句话说,我只想替换段落中不在引号内的单词:

import sys, os.path, win32com.client 
app = win32com.client.Dispatch('Word.Application') 

doc = app.Documents.Open(input_fil1) 
incontent = doc.Content.Text app.Quit() 

# want to replace what's is not inside the quote. 
otcontent = incontent.replace('No', 'Yes') 

print "New content:" print otcontent 
4

1 回答 1

0

根据引号分隔符对文本进行切片,并仅处理位置为 pair 的子字符串。

string = '''Even though "lorem ipsum" may arouse curiosity because of its resemblance to classical Latin, it is not intended to have meaning. If text is comprehensible in a document, people tend to focus on the textual content rather than upon overall presentation. Therefore publishers use lorem ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text. In spite of its basis in Latin, the use of lorem ipsum is often referred to as greeking, from the phrase "it's all Greek to me", which indicates that something is not meant to be readable text (although greeking can also mean somewhat different things; see the article for details)'''

l = string.split('"')
new_l = []
for position,substring in list(enumerate(l)):
    if position%2 == 0:
        s = substring.replace('lorem','STACK OVERFLOW')
        new_l.append(s)
    else:
        new_l.append(substring)


new_string = '"'.join(new_l)

print new_string

输出 :

Even though "lorem ipsum" may arouse curiosity because of its resemblance to classical Latin, it is not intended to have meaning. If text is comprehensible in a document, people tend to focus on the textual content rather than upon overall presentation. Therefore publishers use STACK OVERFLOW ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text. In spite of its basis in Latin, the use of STACK OVERFLOW ipsum is often referred to as greeking, from the phrase "it's all Greek to me", which indicates that something is not meant to be readable text (although greeking can also mean somewhat different things; see the article for details)

第一个“lorem”被忽略,因为它在引用中。

于 2012-11-29T21:47:25.980 回答