這是我的檔案的頭部樣本:
1.1.1.0 - 1.1.1.255
2.2.2.0 - 2.2.2.255
3.3.3.0 - 3.3.3.100
這是我的 Python 正則運算式代碼:
regex = r'(?P<start>\w \.\w \.\w \.\w ) \- (?P<end>\w \.\w \.\w \.\w )'
with open('sorted_french_ips.txt', 'r') as file:
with open('sorted_french_edited_ips.txt', 'a') as sorted_french_edited_ips:
for line in file:
find = re.match(regex, line)
start = find.group('start')
end = find.group('end')
cidr = netaddr.iprange_to_cidrs(start, end)
cidr = str(cidr)
cidr = cidr.replace(', IPNetwork', '\n')
cidr = cidr.replace('[IPNetwork(\'', '')
cidr = cidr.replace('\')]', '')
cidr = cidr.replace('(\'', '')
cidr = cidr.replace('\')', '')
sorted_french_edited_ips.write(f'{cidr}\n')
當我在 Windows 中運行此代碼時,我收到此錯誤:
Traceback (most recent call last):
File "C:\Users\Saeed\Desktop\python-projects\win.py", line 131, in <module>
write()
File "C:\Users\Saeed\Desktop\python-projects\win.py", line 81, in write
start = find.group('start')
AttributeError: 'NoneType' object has no attribute 'group'
但如果我在 Linux 中運行相同的代碼,它就可以正常作業。
為什么我在 Windows 中出現錯誤?Window 的正則運算式和 Linux 不同嗎?應該改變嗎?
uj5u.com熱心網友回復:
您的輸入文本檔案是使用位元組順序標記 BOM 編碼的 Unicode。在 Windows 中用 Python 打開檔案時,默認使用系統編碼,這與 Unicode 不同,將 BOM 位元組序列作為文本的一部分讀入。
如果匹配出現在字串的開頭,該re.match函式只會找到匹配。
第一個line不是從您定義的模式開始,而是從 BOM 序列開始。
要解決此問題,請確保您使用正確的編碼讀取檔案。
如果您的檔案是 UTF-8 BOM 編碼的,請使用
with open('sorted_french_ips.txt', 'r', encoding="utf-8-sig") as file:
在 Linux 中,通常,默認編碼是沒有 BOM 的 UTF-8,因此它可以在不encoding顯式設定引數的情況下作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485682.html
