[Tutor] Re: Splitting up the input

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu Jan 23 11:13:01 2003


On Thu, 23 Jan 2003, Derrick 'dman' Hudson wrote:

> On Thu, Jan 23, 2003 at 02:22:15PM -0000, Deirdre Hackett wrote: | I
> want to read in infromation from the serial port. The probem is i | can
> take i the whole line but only want to read it in in lumps of 9 |
> characters.
>
> | As in, I want to read the first 9 characters and put that into X, |
> then read in the next 9 characters and put that into Y...
>
> There are a couple of ways to do that with varying tradeoffs :
>
> (assume 'f' is a file object referring to the serial port; if you use
> windows I can't help with getting that object)
>
> line = f.readline()
> X = line[:9]
> Y = line[9:18]


Hello!

It might be a better idea to avoid readline() on a "binary" stream, since
there's a chance that there won't be any newlines in the file.  In a worse
case, if there aren't any newlines in this large file, then Python will
waste a lot of effort trying to pull all the characters into memory.

The read() method is much safer since we know that we want to pull chunks
of 9 bytes (characters) at a time.




> This will fail (with an IndexError) if the line is too short.

No, the slicing above should not fail.  For example,

> line = f.readline()
> X = line[:9]
> Y = line[9:18]

Here, Python will assign an empty string to 'Y' if the 'line' is too short
for the slice.  For example:

###
>>> line = "this is a test"
>>> x = line[0:20]
>>> y = line[20:100]
>>> x
'this is a test'
>>> y
''
###

This is because something like 'line[20:100]' asks Python: "Give me a
string containing all the characters between indices 20 and 100".  But
there are no characters that span, so we get back the empty string.
(It's analogous to the "vacuously true" sort of situation that
mathematicians run into every so often.)


Hope that clarifies things!