首页 > 解决方案 > 如何解决 ValueError:使用 re.findall 提取信息时,值的长度与索引的长度不匹配

问题描述

我正在尝试使用 re.findall 从很长的字符串中提取一些信息。但是我遇到了 ValueError:值的长度与索引的长度不匹配。我不确定我必须纠正什么才能使此声明生效。感谢是否有人可以给我一些指导。

df['Part Number']=re.findall(r"^\D{3}\d{4}\D{2}\d{2}\w",str(df["Path"]))

字符串示例如下: V:\DB\CAPGrind\BEFORE\8_INCHES\WG11_5950302.1_5950302.1_A_314696_SAVE_2019_12_02.csv

path=r'V:\DB\CAPGrind\BEFORE\8_INCHES'

files=glob.glob(path+"/*.csv")

df=None 

    for i, f in enumerate (files) :
        if i==0:
            df= np.transpose(pd.read_csv(f,delimiter="|",index_col=False))
                df['Path'] =f
                df['Machine No']=re.findall("WG-11",str(df["Path"]))
                df['Process']= re.findall("CAPGrind",str(df["Path"]))

            # Not Working. Will return ValueError: Length of values does not match length of index when running. The regular expression should be correct.
            df['Part Number']=re.findall(r'"^\D{3}\d{4}\D{2}\d{2}\w",str(df["Path"]))

标签: pythonfindall

解决方案


推荐阅读