Интересно, можете ли вы посоветовать - я получаю приведенную ниже ошибку при обработке списка предметов.Я должен отметить, что этот скрипт работает для 99% элементов - поскольку я расширил список до 84 миллионов строк, я теперь получаю эту проблему.
Я делаю это для каждой строки
elif len(str(x)) > 3 and str(x[len(x)-2]).rstrip() in cdns:
Итак, я не вижу, как индекс может выходить за пределы диапазона, если я активно проверяю, превышает ли он определенную длину перед обработкой?
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-2-a28be4b396bd> in <module>()
21 elif len(str(x)) > 4 and str(x[len(x)-2]).rstrip() in cdns:
22 cleandomain.append(str(x[len(x)-3])+'.'+str(x[len(x)-2])+'.'+str(x[len(x)-1]))
---> 23 elif len(str(x)) > 5 and str(x[len(x)-3]).rstrip() in cdns:
24 cleandomain.append(str(x[len(x)-4])+'.'+str(x[len(x)-3])+'.'+str(x[len(x)-2])+'.'+ str(x[len(x)-1]))
25 #if its in the TLD list, do this
IndexError: list index out of range
Полный цикл ниже,так что я ожидал бы, что если бы элемент списка индекса был вне диапазона, он просто выполнил бы другую команду и напечатал бы значение списка?
for x in index:
#if it ends with a number, it's an IP
if str(x)[-1].isnumeric():
cleandomain.append(str(x[0])+'.'+str(x[1])+'.*.*')
#if its in the CDN list, take a subdomain as well
elif len(str(x)) > 3 and str(x[len(x)-2]).rstrip() in cdns:
cleandomain.append(str(x[len(x)-3])+'.'+str(x[len(x)-2])+'.'+str(x[len(x)-1]))
elif len(str(x)) > 4 and str(x[len(x)-3]).rstrip() in cdns:
cleandomain.append(str(x[len(x)-4])+'.'+str(x[len(x)-3])+'.'+str(x[len(x)-2])+'.'+ str(x[len(x)-1]))
#if its in the TLD list, do this
elif len(str(x)) > 3 and str(x[len(x)-2]).rstrip()+'.'+ str(x[len(x)-1]).rstrip() in tld:
cleandomain.append(str(x[len(x)-3])+'.'+str(x[len(x)-2])+'.'+ str(x[len(x)-1]))
elif len(str(x)) > 2 and str(x[len(x)-1]) in tld:
cleandomain.append(str(x[len(x)-2])+'.'+ str(x[len(x)-1]))
#if its not in the TLD list, do this
else:
cleandomain.append(x)
X генерируется как показано ниже:
X - это список списков - разделенные части домена, как показано ниже [['' news ',' bbc ',' co ',' uk '], [' graph ',' facebook ',' com ']]
import pandas as pd
path = "Desktop/domx.csv"
df = pd.read_csv(path, delimiter=',', header='infer', encoding = "ISO-8859-1")
df2 = df[((df['domain'] != '----'))]
df3 = df2[['domain', 'use']]
for row in df2.iterrows():
index = df3.domain.str.split('.').tolist()
Любая помощь будет отличной