为什么我的Python正则表达式与Windows中的换行符\r\n不匹配?

2024-10-16 20:39:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还是相当陌生的,我的一个正则表达式遇到了问题。我在网上对此进行了研究,并用Python尝试了很多东西,但我还是被卡住了。因为我使用的是Windows,所以我希望\r\n在文本文件中匹配一个新的换行符,因为在Windows中,行是这样终止的。但我发现只有\n个匹配项。为什么呢

这是我的代码(使用\r\n,不匹配)

filename = 'C:\\Users\\jason\\OneDrive\\Documents\\LTspice_my_work\\example_ac_analysis_2.raw'
with open (filename, 'r' ) as f:
    content = f.read()
    print(content)
    pattern3 = r'Variables:\r\n(.*)Values:' 
    print("Here's what matches:")
    text = re.search( pattern3,content,re.DOTALL).group(1)
    print(text)

返回:

Command: Linear Technology Corporation LTspice XVII
Variables:
        0       frequency       frequency
        1       V(v1)   voltage
        2       V(vout) voltage
        3       I(C1)   device_current
        4       I(R1)   device_current
        5       I(V1)   device_current
Values:
0               1.000000000000000e+000,0.000000000000000e+000
        2.000000000000000e+000,0.000000000000000e+000
        1.998028025380720e+000,-6.276990166202591e-002
        3.943949238559487e-007,1.255398033240518e-005
        -3.943949238559341e-007,-1.255398033240518e-005
        -3.943949238559568e-007,-1.255398033240518e-005
1               3.162277660168380e+000,0.000000000000000e+000
        2.000000000000000e+000,0.000000000000000e+000
        1.980453705393099e+000,-1.967499214255068e-001
        3.909258921380289e-006,3.934998428510137e-005
        -3.909258921380277e-006,-3.934998428510137e-005
        -3.909258921380287e-006,-3.934998428510137e-005


Here's what matches:
Traceback (most recent call last):

  File "C:\Users\jason\OneDrive\Documents\Python\Python_scripts\example_ltspice_pytool.py", line 176, in <module>
    text = re.search( pattern3,content,re.DOTALL).group(1)

AttributeError: 'NoneType' object has no attribute 'group'

但是,当我仅使用\n时,我得到了与此代码匹配的代码

filename = 'C:\\Users\\jason\\OneDrive\\Documents\\LTspice_my_work\\example_ac_analysis_2.raw'
with open (filename, 'r' ) as f:
    content = f.read()
    print(content)
    pattern3 = r'Variables:\n(.*)Values:' 
    print("Here's what matches:")
    text = re.search( pattern3,content,re.DOTALL).group(1)
    print(text)

返回


Command: Linear Technology Corporation LTspice XVII
Variables:
        0       frequency       frequency
        1       V(v1)   voltage
        2       V(vout) voltage
        3       I(C1)   device_current
        4       I(R1)   device_current
        5       I(V1)   device_current
Values:
0               1.000000000000000e+000,0.000000000000000e+000
        2.000000000000000e+000,0.000000000000000e+000
        1.998028025380720e+000,-6.276990166202591e-002
        3.943949238559487e-007,1.255398033240518e-005
        -3.943949238559341e-007,-1.255398033240518e-005
        -3.943949238559568e-007,-1.255398033240518e-005
1               3.162277660168380e+000,0.000000000000000e+000
        2.000000000000000e+000,0.000000000000000e+000
        1.980453705393099e+000,-1.967499214255068e-001
        3.909258921380289e-006,3.934998428510137e-005
        -3.909258921380277e-006,-3.934998428510137e-005
        -3.909258921380287e-006,-3.934998428510137e-005


Here's what matches:
        0       frequency       frequency
        1       V(v1)   voltage
        2       V(vout) voltage
        3       I(C1)   device_current
        4       I(R1)   device_current
        5       I(V1)   device_current

提前谢谢你的帮助


Tags: textreheredevicecontentcurrentvariablesfilename
2条回答

当您以文本模式(默认)打开文件时,\r\n在您读取文件时会自动转换为\n,因此您不必担心您使用的是什么操作系统

默认情况下,Python以通用换行模式处理文本文件。从the docs引用:

newline controls how line endings are handled. It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

  • When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.

简而言之,在您接收字符串时,字符串中没有包含\r。如果希望他们保留\r,请更改open调用以添加newline=''(因为csv模块需要这样做,因为行尾是CSV方言的一部分,它需要原始的、未翻译的结尾来正确处理输入)

相关问题 更多 >