[Tutor] removing line ends from Word text files (original) (raw)

David Rock david at graniteweb.com
Sat Jul 17 18:54:33 CEST 2004


OK, a couple things... readline is NOT a Unix-only thing. I just tried it on my XP box and it's fine. open is also an older way of doing things with opening files, as of 2.2, file is probably what you want.

http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-25

and for the sake of completeness, here is the info about built-in file objects: http://www.python.org/doc/current/lib/bltin-file-objects.html

So this: fo = open("filename") line = fo.readline() print repr(line)

becomes this: fo = file("filename") line = fo.readline() print repr(line)

as for interactive Python, I have recently been introduced to ipython and it's great. It has a LOT of features that aren't in the normal shell: http://ipython.scipy.org/

And finally, ^M is decimal 13 (hex 0D), \n is 10, and \r is 13 ... hmm, I guess that means ^M == \r

One thing that I have used over the years to strip newline chars off lines is this, it's not the prettiest, but you'll get the idea:

if '\n' in line:
    line = line[:-1]
if '\r' in line:
    line = line[:-1]

basically, it's assuming (in the case of Windows) that the file ends with '\r\n', and strips them off one at a time.

-- David Rock david at graniteweb.com -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20040717/7ebf04f4/attachment.pgp



More information about the Tutor mailing list