python - Unable to index into a list -


i writing program read output of program, read line line , put in list.

#!/usr/bin/python  import subprocess  def receive(command):     process = subprocess.popen(command, stdout=subprocess.pipe)     lines = iter(process.stdout.readline, "")     line in lines:         recarr = line.split()         print recarr[14]  receive(["receivetest","-f=/dev/pcan32"]) 

the output receivetest program is:

19327481.401 receivetest: m s 0x0000000663 8 2f 00 42 02 00 e4 8a 8a 19327481.860 receivetest: m s 0x000000069e 8 00 1f 5e 28 34 83 59 1a 

it constant stream of messages. when split, list has range of 14 because after splitting, make sure, used:

print len(recarr) 

this gave me output of 14.

but whenever try print last element:

print recarr[14] 

i following error:

file "./cancheck.py", line 10, in receive print recarr[14] indexerror: list index out of range 

this caused erronious text printed @ top of list, need way of making sure program reads in lines start with

1234567.123 /^(.......\.\d{1,3}) (.*)$/ 

any ideas?

based on sample data provided, length of recarr 14.

14 size of list, not maximum index. final element of array, can try recarr[13] list, or recarr[-1] in general.

the reason in python, in programming languages, array indices zero-based: first element accessed recarr[0], second recarr[1], , on. so, 14th element (or last one, in case) accessed recarr[13].

so, loop this:

for line in lines:     recarr = line.split()     print recarr[13] # or recarr[-1] 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -