Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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中保留str.replace函数中的引号短语字符串_Python_Python 2.7 - Fatal编程技术网

在Python中保留str.replace函数中的引号短语字符串

在Python中保留str.replace函数中的引号短语字符串,python,python-2.7,Python,Python 2.7,换句话说,我只想替换段落中不在引号内的单词: 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. otcon

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

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 

根据引号分隔符分割文本,并仅处理位置为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”被忽略,因为它在引号中

你能给我们一些输入和预期输出的例子吗?到目前为止,您尝试了什么?导入sys、os.path、win32com.client app=win32com.client.Dispatch'Word.Application'doc=app.Documents.Openinput\u fil1 incontent=doc.Content.Text app。退出要替换报价中不包含的内容。otcontent=incontent。替换“否”、“是”打印新内容:打印otcontent要替换报价中不包含的内容。otcontent=incontent。替换“否”、“是”打印新内容:打印otcontent您可以通过您的问题添加该信息。我已将您的字符串保存到文件*.docx中,并使用您的脚本进行了测试,效果非常好!:谢谢如果你觉得这个答案满足你的需要,点击我答案右上角的箭头:这样问题就结束了。