Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x Python3-搜索和替换文本文件-_Python 3.x - Fatal编程技术网

Python 3.x Python3-搜索和替换文本文件-

Python 3.x Python3-搜索和替换文本文件-,python-3.x,Python 3.x,我正在使用脚本搜索和替换逗号分隔的文本文件上的字符串 用“不可用”替换“可用”效果很好 但是,如果字符串包含单引号和/或逗号等字符,则会失败 例如: 替换:“可用”、“6ba8c817-d72d-4593-8914-7a40d733b6db”、“免费” 带有:“不可用”、“6ba8c817-d72d-4593-8914-7a40d733b6db”、“豪尔赫” 我尝试了转义引号和逗号,但没有成功 updateBook = updateBook.replace("\'available\'\, \'

我正在使用脚本搜索和替换逗号分隔的文本文件上的字符串

用“不可用”替换“可用”效果很好

但是,如果字符串包含单引号和/或逗号等字符,则会失败

例如:

替换:“可用”、“6ba8c817-d72d-4593-8914-7a40d733b6db”、“免费”

带有:“不可用”、“6ba8c817-d72d-4593-8914-7a40d733b6db”、“豪尔赫”

我尝试了转义引号和逗号,但没有成功

updateBook = updateBook.replace("\'available\'\, \'6ba8c817-d72d-4593-8914-7a40d733b6db\'\, \'free", 
"\'not-available\'\, \'6ba8c817-d72d-4593-8914-7a40d733b6db\'\, \'jorge")
下面是代码

# Read in the file
updateBook = None
with open('books.txt', 'r') as f:
    updateBook = f.read()

# Replace the target string

updateBook = updateBook.replace("'available', '6ba8c817-d72d-4593-8914-7a40d733b6db', 'free\n'", 
"'not-available', '6ba8c817-d72d-4593-8914-7a40d733b6db', 'jorge'")


# Write changes to the file.
with open('books.txt', 'w') as f:
    f.write(updateBook)
如果你能帮忙,请现在就告诉我。谢谢

我可以在这里帮助你。这取决于您需要更换的部件的通用性

import re


def repl(match):
    d = match.groupdict()
    d.update('one': 'non-available', 'two': '6ba8c817-d72d-4593-8914-7a40d733b6db', 'three': 'jorge')
    s = '{preone}{one}{pretwo}{two}{prethree}{three}{postthree}'.format(**d)


original_string = 'available...'
escaped_values = map(re.escape, ['available', '6ba8c817-d72d-4593-8914-7a40d733b6db', 'free'])
regex_string = r'^(?P<preone>[\'\"])(?P<one>{0})(?P<pretwo>[\'\"],\s*[\'\"])(?P<two>{1})(?P<prethree>[\'\"],\s*[\'\"])(?P<three>{2})(?P<postthree>[\'\"])$'.format(*escaped_values)
replaced_string re.sub(regex_string, repl, original_string)
重新导入
def repl(匹配):
d=match.groupdict()
d、 更新(‘一’:‘不可用’,‘二’:‘6ba8c817-d72d-4593-8914-7a40d733b6db’,‘三’:‘豪尔赫’)
s='{preone}{one}{pretwo}{two}{prethree}{three}{postthree}'。格式(**d)
原始字符串='可用…'
转义值=映射(关于转义,['available','6ba8c817-d72d-4593-8914-7a40d733b6db','free'])
正则表达式字符串=r'^(?P[\'\'\'](?P{0})(?P[\'\'\'],\s*[\'\'\'])(?P{1})(?P[\'\'\'],\s*[\'\'\'])(?P{2})(?P[\'\'\'])$”。格式(*转义值)
替换的字符串re.sub(正则表达式字符串、repl、原始字符串)
可能会对您有所帮助。这取决于您需要更换的部件的通用性

import re


def repl(match):
    d = match.groupdict()
    d.update('one': 'non-available', 'two': '6ba8c817-d72d-4593-8914-7a40d733b6db', 'three': 'jorge')
    s = '{preone}{one}{pretwo}{two}{prethree}{three}{postthree}'.format(**d)


original_string = 'available...'
escaped_values = map(re.escape, ['available', '6ba8c817-d72d-4593-8914-7a40d733b6db', 'free'])
regex_string = r'^(?P<preone>[\'\"])(?P<one>{0})(?P<pretwo>[\'\"],\s*[\'\"])(?P<two>{1})(?P<prethree>[\'\"],\s*[\'\"])(?P<three>{2})(?P<postthree>[\'\"])$'.format(*escaped_values)
replaced_string re.sub(regex_string, repl, original_string)
重新导入
def repl(匹配):
d=match.groupdict()
d、 更新(‘一’:‘不可用’,‘二’:‘6ba8c817-d72d-4593-8914-7a40d733b6db’,‘三’:‘豪尔赫’)
s='{preone}{one}{pretwo}{two}{prethree}{three}{postthree}'。格式(**d)
原始字符串='可用…'
转义值=映射(关于转义,['available','6ba8c817-d72d-4593-8914-7a40d733b6db','free'])
正则表达式字符串=r'^(?P[\'\'\'](?P{0})(?P[\'\'\'],\s*[\'\'\'])(?P{1})(?P[\'\'\'],\s*[\'\'\'])(?P{2})(?P[\'\'\'])$”。格式(*转义值)
替换的字符串re.sub(正则表达式字符串、repl、原始字符串)

我希望你不是在重新发明。
\n
可能会绊倒你,因为它在
'free\n'
的结束单引号内。不确定这是否是故意的,但如果没有看到
f.read()
读入的实际行,很难判断。如果在这些单引号中确实有一个文字
\n
,请使用双反斜杠来避开反斜杠:“…,”free\\n`。我希望你不是在重新发明。
\n
可能会绊倒你,因为它位于
'free\n'
的结束单引号内。不确定这是否意味着这样做,但如果看不到由
f.read()
读入的实际行,则很难判断。如果这些单引号中确实存在文字
\n
,请使用双反斜杠来转义反斜杠:`,'free\\n`。