Python re.search异常

时间:2016-08-08 06:42:17

标签: python search substring

我有一个例程,搜索文件目录并从文件名中提取客户编号:

import os
import re

suffix= '.csv'

# For each file in input folder, extract customer number 
input_list = os.listdir(path_in)
for input_file in input_list:
        fileInput = os.path.join(path_in,input_file)
        customer_ID = re.search('custID_(.+?)'+suffix,fileInput).group(1)
        print(customer_ID)

使用suffix='.csv'和一个包含csv文件的文件夹:

  

avg_hrly_custID_8147611.csv,avg_hrly_custID_8147612.csv,avg_hrly_custID_8147613.csv ...

我得到了预期的输出:

  

8147611,   8147612,   8147613 ...

但是,suffix = '.png'和.png图像文件的文件夹:

  

yearly_average_plot_custID_8147611.png,   yearly_average_plot_custID_8147612.png,   yearly_average_plot_custID_8147613.png   ...

我收到此错误:

  

AttributeError:'NoneType'对象没有属性'group'

为什么它不能用于图像文件?

1 个答案:

答案 0 :(得分:0)

@BrenBarn发现了问题的原因。正则表达式失败,因为目录中有一个名称不匹配的子目录。我通过介绍try....except

解决了这个问题
import os
import re

suffix= '.png'

# For each file in input folder, extract customer number 
input_list = os.listdir(path_in)
for input_file in input_list:
        fileInput = os.path.join(path_in,input_file)
        try:
           customer_ID = re.search('custID_(.+?)'+suffix,fileInput).group(1)
           print(customer_ID)
        except:
           pass