错误:
string index out of range' in line[0]=='1'
代码:
r = requests.post("http://www.anbima.com.br/informacoes/est-termo/CZ-down.asp",
data={'Dt_Ref': date.strftime("%d/%m/%Y"), 'saida': 'txt', 'Idioma': 'US'})
parametros = [] # primeira tabela
ETTJ = [] # segunda tabela
for line in r.text.split('\r\n')[1:-1]:
linesplit = line.split('@')[1:]
# convertendo campos vazios para nan
for idx, e in enumerate(linesplit):
if e in ['', '--']:
linesplit[idx] = np.nan
if line[0] == '1':
parametros.append(linesplit)
elif line[0] == '2':
ETTJ.append(linesplit)
if len(parametros)<=0 or len(ETTJ)<=0:
return False, False
parametros = pd.DataFrame(parametros[1:], columns=parametros[0])
ETTJ = pd.DataFrame(ETTJ[2:], columns=ETTJ[1])
ETTJ['Business Day'] = ETTJ['Business Day'].str.replace(',', '')
ETTJ['Business Day'] = ETTJ['Business Day'].astype(int)
for column in ['Indexed', 'Prefixed', 'BEI']:
ETTJ[column] = ETTJ[column].astype(float).fillna(0.0)
for column in ['Beta 1', 'Beta 2', 'Beta 3', 'Beta 4', 'Lambda 1', 'Lambda 2']:
parametros[column] = parametros[column].astype(float).fillna(0.0)
答案 0 :(得分:0)
行为空。这就是为什么字符串索引超出范围
答案 1 :(得分:0)
您可以在索引line
之前检查是否有任何字符:
for line in r.text.split('\r\n')[1:-1]:
linesplit = line.split('@')[1:]
# convertendo campos vazios para nan
for idx, e in enumerate(linesplit):
if e in ['', '--']:
linesplit[idx] = np.nan
if len(line) == 0:
pass #or whatever is appropriate
elif line[0] == '1':
parametros.append(linesplit)
elif line[0] == '2':
ETTJ.append(linesplit)