使用Mac Automator运行Python查找&;代替多行(\n\r)不工作

使用Mac Automator运行Python查找&;代替多行(\n\r)不工作,python,macos,automator,find-replace,Python,Macos,Automator,Find Replace,我正在使用Mac Automator运行“查找并替换”python脚本。(下面是一个简化版本。)除了换行之外,它工作得非常好 我尝试了多个\r\n\r\n变体,但没有删除换行符 replacements = { 'class="spec-container"':'class="row"', '</span>\n\n':'</span>', '</div>\n\n\n':'</div>\n' } wi

我正在使用Mac Automator运行“查找并替换”python脚本。(下面是一个简化版本。)除了换行之外,它工作得非常好

我尝试了多个\r\n\r\n变体,但没有删除换行符

    replacements = {
    'class="spec-container"':'class="row"',
    '</span>\n\n':'</span>',
    '</div>\n\n\n':'</div>\n'
    }

    with open('/../TEMP/INPUT.txt') as infile, 
    open('/../TEMP/OUTPUT.txt', 'w') as outfile:
    for line in infile:
        for find, replace in replacements.iteritems():
            line = line.replace(find, replace)
        outfile.write(line)
replacements={
“class=”spec container“:“class=”行“,
“\n\n”:”,
“\n\n\n”:“\n”
}
以open('/../TEMP/INPUT.txt')作为填充,
打开('/../TEMP/OUTPUT.txt',w')作为输出文件:
对于填充中的线:
对于查找,请在替换中替换。iteritems():
行=行。替换(查找,替换)
输出文件。写入(行)

真的只有找到我的脚与蟒蛇,所以道歉提前。但是感谢您的帮助。

您可以使用
strip()
方法删除Python中字符串周围的空白<代码>“hello\n”。strip()将返回
“hello”
。如果您只想将字符串的一侧剥离,也可以使用
lstrip()
rstrip()