This is my first program in Python
Prateep Siamwalla
teep at inet.co.th
Thu Jan 18 04:21:43 EST 2001
I tried your program on my PythonWin editor, and it worked for me. What
error message did you get?
contents of file c:\blank\globs.txt:
one:alpha
two:beta
three:gamma
>>> def trythisout():
... f = open(r'c:\blank\globs.txt','r')
... for line in f.readlines():
... (first,second) = string.split(line,":",1)
... print first
... f.close()
...
>>> trythisout()
one
two
three
did you get a "ValueError: unpack list of wrong size" error? If you did,
then one of your input lines probably is missing a ":"
hope this helps.
teep
Richard Zilavec <rzilavec at tcn.net> wrote in message
news:3a66a585.268350144 at news.tcn.net...
>
> I've spent the last few weeks trying to find a good book on Python.
> Seems that most stores don't carry a large selection, however I found
> The Quick Python Book.. The material seems to flow in the right
> direction and the fonts are easy on the eyes.
>
> Anyways, I wrote my first program but was stumped on readline(). I
> could get the first line, but not the rest of the file. I tried
> searching python.org and could not find an example.... well here it
> is:
>
> import string
> file = open("/tmp/globs", 'r')
> for line in file.readlines():
> (first,second) = string.split(line,':',1)
> print first
> file.close()
>
> This was my first attempt, which looks like it should work but failed:
> invalid syntax. Is this not possible?
> while line = file.readlines():
>
> Am I using string.split properly?
>
> I was quite happy with how easy this was to put together, I'm just not
> sure if its totally correct.
>
> --
> Richard Zilavec
> rzilavec at tcn.net
More information about the Python-list
mailing list