kavya
kavya

Reputation: 789

TypeError: list indices must be integers, not unicode in python code

I used the split() function to convert string to a list time = time.split() and this is how my output looks like :

[u'1472120400.107']
[u'1472120399.999']
[u'1472120399.334']
[u'1472120397.633']
[u'1472120397.261']
[u'1472120394.328']
[u'1472120393.762']
[u'1472120393.737']

Then I tried accessing the contents of the list using print time[1] which gives the index out of range error (cause only a single value is stored in one list). I checked questions posted by other people and used print len(time). This is the output for that:

1
[u'1472120400.107']
1
[u'1472120399.999']
1
[u'1472120399.334']
1
[u'1472120397.633']
1
[u'1472120397.261']
1
[u'1472120394.328']
1
[u'1472120393.762']
1
[u'1472120393.737']

I do this entire thing inside a for loop because I get logs dynamically and have to extract out just the time. This is part of my code:

line_collect = lines.collect() #spark function
for line in line_collect :
    a = re.search(rx1,line)
    time = a.group()
    time = time.split()
    #print time[1] #index out of range error which is why I wrote another for below
    for k in time :
        time1 = time[k]#trying to put those individual list values into one variable but get type error
        print len(time1)

I get the following error :

time1 = time[k]
TypeError: list indices must be integers, not unicode

Can someone tell me how to read each of those single list values into just one list so I can access each of them using a single index[value]. I'm new to python.

My required output:

time =['1472120400.107','1472120399.999','1472120399.334','1472120397.633','1472120397.261','1472120394.328','1472120393.762','1472120393.737']

so that i can use time[1] to give 1472120399.999 as result.

Upvotes: 1

Views: 3175

Answers (3)

Kafo
Kafo

Reputation: 3656

Update: I misunderstood what you wanted. You have the correct output already and it's a string. The reason you have a u before the string is because it's a unicode string that has 16 bits. u is a python flag to distinguish it from a normal string. Printing it to the screen will give you the correct string. Use it normally as you would any other string.

time = [u'1472120400.107']  # One element just to show

for k in time:
    print(k)

Upvotes: 1

kavya
kavya

Reputation: 789

Thanks for your help. I finally got the code right:

newlst = []
for line in line_collect :
    a = re.search(rx1,line)
    time = a.group()
    newlst.append(float(time))
print newlst

This will put the whole list values into one list. Output:

[1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633, 1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]

Upvotes: 0

Yam Mesicka
Yam Mesicka

Reputation: 6581

Looping over a list using a for loop will give you one value at a time, not the index itself. Consider using enumerate:

for k, value in enumerate(time):
    time1 = value # Or time1 = time[k]
    print(time1)

Or just getting the value itself:

for k in time:
    time1 = k
    print(time1)

--

Also, Python is zero based language, so to get the first element out of a list you probably want to use time[0].

Upvotes: 0

Related Questions