Python 多个子正则表达式

标签 python python-3.x regex

最初使用这样的工作脚本来检查文件夹中的 csv 文件并替换子字符串:

import fileinput
import os
import glob

#### Directory and file mask
this = r"C:\work\PythonScripts\Replacer\*.csv"
output_folder = "C:\\work\\PythonScripts\\Replacer\\"

#### Get files
files = glob.glob(this)

#### Section to replace
text_to_search = 'z'
replacement_text = 'ZZ_Top'

#### Loop through files and lines:
for f in files:
    head, tail = os.path.split(f)
    targetFileName = os.path.join(head, output_folder, tail)

    with fileinput.FileInput(targetFileName, inplace=True, backup='.bak') as file:
        for line in file:
            print(line.replace(text_to_search, replacement_text), end='')

有必要替换几个 Word 引号和长连字符。所以我想到在上面的循环中使用类似的东西:

s = '’ ‘ ’ ‘ ’ – “ ” “ – ’'
print(s)
print(s.replace('’', '\'').replace('‘', '\'').replace('–','-').replace('“','"').replace('”','"'))

==>

’ ‘ ’ ‘ ’ – “ ” “ – ’
' ' ' ' ' - " " " - '

但是后来我遇到了以下关于使用正则表达式子函数的评论: https://stackoverflow.com/a/765835

所以我尝试了一下,它自己运行得很好:

import re

def multisub(subs, subject):
 #   "Simultaneously perform all substitutions on the subject string."
    pattern = '|'.join('(%s)' % re.escape(p) for p, s in subs)
    substs = [s for p, s in subs]
    replace = lambda m: substs[m.lastindex - 1]
    return re.sub(pattern, replace, subject)

print(multisub([('’', '\''), ('‘', '\''), ('–','-'), ('“','"'), ('”','"')], '1’ 2‘ 1’ 2‘ 1’ 3– 4“ 5” 4“ 3– 2’'))

==>

1' 2' 1' 2' 1' 3- 4" 5" 4" 3- 2'

但是一旦我将其粘贴到原始脚本中,它就会运行但不会修改文件:

import fileinput
import os
import glob
import re

#### Directory and file mask
this = r"C:\work\PythonScripts\Replacer\*.csv"
output_folder = "C:\\work\\PythonScripts\\Replacer\\"

#### RegEx substitution func
def multisub(subs, subject):
 #   "Simultaneously perform all substitutions on the subject string."
    pattern = '|'.join('(%s)' % re.escape(p) for p, s in subs)
    substs = [s for p, s in subs]
    replace = lambda m: substs[m.lastindex - 1]
    return re.sub(pattern, replace, subject)

#### Get files
files = glob.glob(this)

#### Loop through files and lines:
for f in files:
    head, tail = os.path.split(f)
    targetFileName = os.path.join(head, output_folder, tail)

    with fileinput.FileInput(targetFileName, inplace=True, backup='.bak') as file:
        for line in file:
            print(multisub([('’', '\''), ('‘', '\''), ('–','-'), ('“','"'), ('”','"')], line), end='')

这里可能出了什么问题?

最佳答案

你的代码实际上对我来说是有效的,就像我测试它时一样,但是你有很多不必要的处理,可能会引入错误。与常规 open 相比,使用 fileinput 的一大优点是它可以循环访问多个文件中的行,而无需另一个循环来单独打开每个文件。所以试试这个,看看它是否有效:

#### Get files
files = glob.glob(this)

#### Loop through files and lines:
for line in fileinput.input(files, inplace=True, backup='.bak'):
    print(multisub([('’', '\''), ('‘', '\''), ('–','-'), ('“','"'), ('”','"')], line), end='')

关于Python 多个子正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62802584/

相关文章:

python - 我的 Python 脚本在我的字符串中添加了额外不需要的字符

python - 使用OpenCV获得完整的形状图案

python-3.x - 如何使用帧单元制作 TIME 字幕(.srt、.smi 等)

java - 在 Java 中,正则表达式 "\1"(反向引用)不起作用

python - 在静态应用程序引擎网站上运行 python 脚本?

python - PyQt4 强制 View 从 QAbstractItemModel 获取更多

python - 停止执行 Python 脚本

python - 为什么不能在计数排序算法中使用哈希表/字典?

regex - 如何通过正则表达式在 kotlin 中匹配双引号中的字符串

正则表达式 - 如何不匹配两个换行符