[Tutor] Re: Splitting up the input

Derrick 'dman' Hudson dman@dman.ddts.net
Thu Jan 23 10:09:01 2003


--qDbXVdCdHGoSgWSk
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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.=20

| 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 =3D f.readline()
X =3D line[:9]
Y =3D line[9:18]

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


Another way is to only read in 9 characters :

X =3D f.read(9)
Y =3D f.read(9)

This will yield different results if the end of the input stream is
encountered before 9 characters have been read.  It will also count CR
and LF as part of the 9 characters.


In all cases you need to check the input to make sure you have what
you expect to have before proceeding and deal with the situation if
something isn't right.=20

HTH,
-D

--=20
"He is no fool who gives up what he cannot keep to gain what he cannot lose=
."
        --Jim Elliot
=20
http://dman.ddts.net/~dman/

--qDbXVdCdHGoSgWSk
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj4wBWsACgkQO8l8XBKTpRTIuwCbBn63AAUz2b7dKgzgW6mplxXb
htYAoI+N3BzscFOjwolDeS46IWaHfOMc
=BbKO
-----END PGP SIGNATURE-----

--qDbXVdCdHGoSgWSk--