gpt4 book ai didi

python - 如何在 python 上循环而不出现 IndexError ?

转载 作者:行者123 更新时间:2023-12-01 03:52:52 24 4
gpt4 key购买 nike

我尝试循环获取url_dataurl_data 是一个字符串数组。我收到一个 IndexError 内容如下:

keywords[i]=urlparse.urlparse(url_data[i])
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

我的代码:

import os
import csv
import numpy as np
import pandas
import urlparse
from numpy import genfromtxt

os.chdir("C:\Users\EDAWES01\Desktop\Cookie profiling")
data = pandas.read_csv('activity_url.csv', delimiter=';')
data_read=np.array(data)
quantity = data_read[:, 2]
url_data = data_read[quantity==1][:,1]
url_data #extract URL data

keywords=[]

for i in url_data:
keywords[i]=urlparse.urlparse(url_data[i])
keywords[i]=keywords[2] #this is the path element
keywords[i]=keywords[i].split("/")

keywords

最佳答案

尝试这个循环

for i in xrange(0,len(url_data)):
keywords[i]=urlparse.urlparse(url_data[i])

关于python - 如何在 python 上循环而不出现 IndexError ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37961494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com