ValueError:找不到子字符串?卡住

时间:2015-10-04 19:40:21

标签: python string pandas substring

df= pd.read_csv('test.csv')
df["Name"] = df["Infraction"] = df["Yards"] = ""
df[["Name", "Infraction", "Yards"]] = map(df.Detail.apply(extract))

这是下面的示例条目。

val = 'Kirk Cousinspass complete short left toJamison Crowderfor 33 yards (tackle byDamontre Moore). Penalty onKory Lichtensteiger: Offensive Holding, 10 yards'

我有以下代码。

df[["Name", "Infraction", "Yards"]] = list(df.Detail.apply(extract))

我有一个我创建的提取方法。

def extract(entry):

split = entry.split(':')
sliced = entry.index('Penalty on') 
new_phrase =  entry[sliced:]
name_index = new_phrase.find(':')
name = new_phrase[:name_index]
second_part = new_phrase[name_index:]
infraction = second_part.split(',')[0].split(':')[1].strip()
yards_penalized = second_part.split(',')[1].strip()

return name[10:], infraction, yards_penalized

我很确定这段代码不是最好的方法,但即便如此我也收到了这个错误。

line 8, in extract
sliced = entry.index('Penalty on') 
ValueError: substring not found

我知道这就是轰炸。我试过用try,catch。可能是错误的,现在就陷入困境。谢谢!

0 个答案:

没有答案