脚本不再工作,.group()过去工作但现在抛出错误

时间:2015-07-27 18:52:47

标签: python

这是造成问题的代码部分:

import pandas as pd
import re

df
df.columns = ['Campaigns', 'Impressions', 'Attempts', 'Spend']
Campaigns = df['Campaigns']
IDs = []
for c in Campaigns:
    num = re.search(r'\d{6}',c).group()
    IDs.append(num)
pieces = [df,pd.DataFrame(IDs)]
frame = pd.concat(pieces, axis=1, join='outer',ignore_index=False)
frame['ID'] = frame[0]
del frame[0]
frame

这是错误:

Error: 'NoneType' object has no attribute 'group'

当我在ipython中单独尝试一切时,一切正常,例如:

in>> test = 'YP_WON2_SP8_115436'
in>> num = re.search(r'\d{6}',test)
in>> num.group()
out>> '115436'

我已尝试按上述方式拆分代码,但仍然会抛出相同的错误。

1 个答案:

答案 0 :(得分:0)

修正了代码:

df
df.columns = ['Campaigns', 'Impressions', 'Attempts', 'Spend']
Campaigns = df['Campaigns']
ID = []
for c in Campaigns:
    m = re.search(r'\d{6}',c)
    if m:
        num = re.search(r'\d{6}',c).group()
        ID.append(num)
    else:
        ID.append('No ID')
pieces = [df,pd.DataFrame(ID)]
frame = pd.concat(pieces, axis=1, join='outer',ignore_index=False)
frame['ID'] = frame[0]
del frame[0]
frame