iterating over lines in a file

nobody no at bo.dy
Sat Jul 22 11:46:07 EDT 2000


cjc26 at nospam.cornell.edu (Cliff Crawford), in
<slrn8nhj0s.jr0.cjc26 at synecdoche.sowrong.org>:
> * nobody <no at bo.dy> menulis:

>> now, this newbie has run into another perl idiom he'd like to figure
>> out how to rewrite in python - "while (<FILE>) { print; }" - and, by
>> extension, how to get python to print the string i hand it, the whole
>> string i hand it, and nothing but the string i hand it?

> Hmm..not quite sure what you mean..maybe you want to use
> sys.stdout.write() instead?

perhaps. unfortunately, i haven't had time to work on this since i made
that posting, so i have got no further; i expect there's some fairly easy
way to write (copy) all the lines from one file to another one, possibly
(though not necessarily) stdout. perl is nice in that this task is a one-
liner in that language; i don't *need* it that concise, but it would be
nice. i like one-liners.

>> i despise machines trying to second- guess my intentions, and silly
>> little print statements trying to be clever with their arguments are
>> nothing more than that.

> print doesn't really try to be clever with its arguments, except for
> printing a space between each one and a newline at the end.

that's trying to be clever. then, if i'm reading raw lines from a file and
want to print them verbatim, i have to strip a newline somewhere somehow?
how do i copy a file to another one, is there a file.copy method in some
module somewhere?

> Maybe what you're looking for is the format operator?

it's certainly useful, but will it stop the print statement from
outputting things i may or occasionally may not want output?

[...]
>> i like it, i just wish its functions would be more consistent about
>> what sort of regexps they want - either all compiled or all not
>> compiled; i'm seeing some wanting one and some the other, for some
>> reason. might be just my system, i suppose...

> The two should be interchangable..AFAIK wherever you can use a compiled
> regexp, you can use an uncompiled one, and vice-versa.

that's what the documentation claims, but here's what i get:

Python 1.5.2 (#1, Feb  1 2000, 16:32:16)  [GCC egcs-2.91.66 19990314/Linux (egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import re
>>> test="this is a test string"
>>> reg=r"est"
>>> reg_c=re.compile(reg)
>>> match=re.match(reg_c,test)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python1.5/re.py", line 40, in match
    return _cachecompile(pattern, flags).match(string)
  File "/usr/lib/python1.5/re.py", line 33, in _cachecompile
    value = compile(pattern, flags)
  File "/usr/lib/python1.5/re.py", line 79, in compile
    code=pcre_compile(pattern, flags, groupindex)
TypeError: argument 1: expected string, instance found
>>> match=re.match(reg,test)
>>>

the compiled one works in re.findall, though. i've no idea why this
bites me in this way, but it's not too hard to work around, so...





More information about the Python-list mailing list