0

I tried to loop for url_data. url_data is an array of strings. I get an IndexError that reads:

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

My code:

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
2
  • 1
    keywords is initialized to an empty list. So there are no valid indices. The loop tries to use i as an index, multiple times, but (1) i apparently isn't an integer and (2) even if it were, it would be out of range since they're all out of range for an empty list. Commented Jun 22, 2016 at 7:44
  • keywords is a error. But the error message is about url_data[i]. url_data and i are str. str[str] won't work Commented Jun 22, 2016 at 7:51

3 Answers 3

1

try this loop instead

for i in xrange(0,len(url_data)):
     keywords[i]=urlparse.urlparse(url_data[i])
Sign up to request clarification or add additional context in comments.

1 Comment

xrange(len(list)) is not the pythonic way.
1

You're doing it wrong. If you want both the element and index, use enumerate().

for idx,url in enumerate(url_data):
    keywords[idx]=urlparse.urlparse(url_data[idx])
    # or it could be
    keywords[idx]=urlparse.urlparse(url)  
    # both of these will still raise IndexError for keywords

Now, coming to the issue of keywords which is an empty list so you'll get IndexError for that too. I think you want to do the following instead.

keywords.append(urlparse.urlparse(url))

Comments

0

From the looks of your code keywords should be either:

keywords = {}
keywords = dict()

print(i) to figure out what you're doing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.