0

I am having a lot of trouble with the list returned by the split function in python. I am probably being incredibly dense.

My python code is as follows

str = "XXX:YYY:ZZZ"
var+=1
ins = str.split(':')
print ins

When the script runs it returns the following result

['XXX','YYY','ZZZ']

What i am struggling to do is pull out the string contained the second string in the list. I would have thought the following code at the end of the python code would work.

print ins[1]

but when i run it i get the following error

IndexError: list index out of range

Any help would be much appreciated

full code

import time
ser = serial.Serial("COM3", 9600)
print ser
time.sleep(3)
print "Sending serial data"
var = 0
while var<=10:
    str = ser.readline()
    print str
    var+=1
    ins = str.split(':')
    print ins
    print ins[0]
    if (str.split(':')=='end\n'):
        break
if(ser.isOpen()):
    print "Serial connection is still open."
    ser.close();
    print "Serial connectionnow terminated."

This returns

Serial<id=0x2a7fc50, open=True>(port='COM3', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
Sending serial data
Program Initiated

['Program Initiated\n']
Program Initiated

0:addsd:1

['0', 'addsd', '1\n']
0
1:addsd:2

['1', 'addsd', '2\n']
1
2:end:2

['2', 'end', '2\n']
2
11
  • 2
    Works for me. Please post your exact code and exact error message (full traceback). Commented Mar 5, 2011 at 18:48
  • 1
    Works perfectly for me. There's code you're not showing us though, var has no purpose, for instance. (It should also have a more descriptive name). Without all the code we can't help you. Commented Mar 5, 2011 at 18:50
  • P.S. your comment says you want to pick the index of the result...care to elaborate on your title in the question? (Or change the title?) Commented Mar 5, 2011 at 18:51
  • @Sven Marnach The full code is a little more complicated as the string it is that i am trying to split is coming from an arduino mega via a comm port. The string appears to be working fine as i have done tests on it. <p>full code will be added above Commented Mar 5, 2011 at 18:59
  • 1
    then ins[1] will not work for the first time through. You'll need to skip it. Commented Mar 5, 2011 at 19:19

2 Answers 2

1

Your code will not work in instances where the input you're analyzing has a length <= 1.

Try checking for that and handling it in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

as you were the first one to pick out my rookie mistake i have accepted your answer. thanks again
0

One of the lines you are reading probably has a two '\n' characters in a row. When that string is read with readlines() and then spit it has no [1] index only a [0] index.

What output do you get for the program:

import serial // Or is serial a builtin for the version of python you are using?
print repr(serial.Serial("COM3", 9600))

Thanks, Marlen

10 Comments

@jklmuk I think there must be two '\n' characters in a row at some point.
output of print repr(ins) is:<br>['Program Initiated\n'] ['0', 'addsd', '1\n']<br> ['1', 'addsd', '2\n']<br> ['2', 'end', '2\n']<br>
@jklmuk readline only reads up to a newline('\n') character if your string was "xxx:\n\n:xxx:yyy" I think your code wouldn't work.
@ marlen t. b. output is 'P' 'r' 'o' 'g' 'r' 'a' 'm' ' ' 'I' 'n' 'i' 't' 'i' 'a' 't' 'e' 'd' '\n' '0' ':' 'a' 'd' 'd' 's' 'd' ':' '1' '\n' '1' ':' 'a' 'd' 'd' 's' 'd' ':' '2' '\n' '2' ':' 'e' 'n' 'd' ':' '2' '\n'
@marlen t. b. your updated answer is correct the first string being returned doesn't have and index 1 which was causing the problem thanks for you help
|

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.