簡體   English   中英

為什么我的 Python 正則表達式與 Windows 中的換行符 \\r\\n 不匹配?

[英]Why does my Python regular expression not match \r\n for newline in Windows?

我對 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

提前感謝您的幫助!

當您以文本模式(默認)打開文件時, \\r\\n會在您讀取文件時自動轉換為\\n ,因此您不必擔心您使用的是什么操作系統。

默認情況下,Python 以通用換行模式處理文本文件。 引用文檔

newline控制如何處理行尾。 它可以是None'''\\n''\\r''\\r\\n' 它的工作原理如下:

  • 從流中讀取輸入時,如果newlineNone ,則啟用通用換行符模式。 輸入中的行可以以'\\n''\\r''\\r\\n'結尾,這些在返回給調用者之前會被轉換為'\\n' 如果是'' ,則啟用通用換行符模式,但行尾將返回給調用者未翻譯。 如果它具有任何其他合法值,則輸入行僅由給定的字符串終止,並且行尾未翻譯地返回給調用者。

因此,在短期,你的字符串沒有\\r通過您收到他們的時間在其中。 如果您希望他們保留\\r ,請更改您的open調用以添加newline=''csv模塊需csv ,因為行尾是 CSV 方言的一部分,它需要原始的未翻譯的結尾來處理輸入正確)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM