我正在解析一个文件,但当我输出匹配到调试时,我发现它输出匹配的单个字符,而不是一次输出整个匹配。我使用pythex.com验证了正则表达式是正确的,并且由于某种原因,pricePatt没有输出任何内容。
代码:
import sys
import re
def main():
if len(sys.argv) < 2:
print 'must include file to read from'
return
with open(sys.argv[1], 'r') as myfile:
data=myfile.read().replace('\n', '')
itemPatt = re.compile('(?![\s\-]?F\s)(?!\s|S?C\s)[a-zA-Z\s]+')
pricePatt = re.compile('\d+[.,]\d{2}')
for m in itemPatt.finditer(data):
print m.group()
return
main()
文件:
CASCADN FARM DARK PC 17.16 F SC CASC BAR VANI 1.29-F SC CASC BAR VANI 1.29-F SC CASC BAR VANI 1.29-F SC CASC BAR VANI 1,29-F 3 @ 1 99 MINUTE RICE PC 5.97 F SC MINUTE RTS 0.32-F SC MINUTE RTS 0.32-F SC MINUTE RTS 0.33-F BERIO OLIVE OIL PC 14.99 F SC BERIO OLIVE OIL 3.60-F 3 @ 3/5.00 UB RICE 5.00 F 2 @ 1.29 HUNT ZSTY/SPCY SC PC 2.58 F SC HUNT SPAG SC 0,29-F SC HUNT SPAG SC 0.29-F GHIRARDELLI CHOC 2.79 F MISSN SM CORN TORT 2.'9 F 4.11 lb @ 0.69 /lb WT 4011 BANANAS 2.84 F STRAWBERRIES PC 4.99 F SC STRAWBERRIES 2.00-F 4899 PARSLEY 1,37 F BLUEBERRIES PC 4.99 F SC BLUEBERRIES 2.00-F 3 @ 5.49 AMYS WHLMEAL 16.17 F ALMOND BREEZE PC 3.39 F SC ALMOND BREEZE 0.39-F 4 @ 1.19 CHOBANI GRK YOG PC 4.76 F SC CHLB SMPY 100 CRCH 0.19-F SC CHLB SMPY 100 CRCH 0.19-F SC CHLB SMPY 100 CRCH 0.19-F