我正在使用 python 并试图从 .txt 文件中提取数字,然后将它们分组到多个类别中。.txt 文件如下所示:
IF 92007<=ZIPCODE<=92011 OR ZIPCODE=92014 OR ZIPCODE=92024
OR 92054<=ZIPCODE<=92058 OR ZIPCODE=92067 OR ZIPCODE=92075
OR ZIPCODE=92083 OR ZIPCODE=92084 OR ZIPCODE=92091 OR ZIPCODE=92672
OR ZIPCODE=92081 THEN REGION=1; ** N COASTAL **;
此代码用于从第一行中提取数字:
import re
TXTPATH = 'C:/zipcode_mapping.txt'
f = open(TXTPATH,'r')
expr= "IF 92007<=ZIPCODE<=92011 OR ZIPCODE=92014 OR ZIPCODE=92024"
for line in f:
L = line
print(L)
matches = re.findall("([0-9]{5})",expr)
for match in matches:
print match
不过,我似乎无法从其他行中提取数字。有什么建议么?