1

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.

2
  • what is your required output? Commented Sep 8, 2016 at 19:35
  • I've added the output to the question. Commented Sep 9, 2016 at 7:04

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

When i print my newlst, it's still empty.
Then i tried in the main for loop: newlst = [] newlst.append(time) print newlst[] but still the values are in different lists.
My original code doesn't put all the values into a single list. Every time a single value is extracted it creates a new list for that. That's why i get index out of bound error for list[1] but get the result for list[0]. It's creating 8 different lists if i'm not wrong.
0

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].

1 Comment

I get the same output as my old one. I've added the required output to my question.
0

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]

1 Comment

I think your original code would have worked since you had an inner loop. You could have converted it to float in the inner loop. Original question shows desired output to be strings.

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.