"\n" in ASCII-file

Bengt Richter bokr at oz.net
Fri Jul 18 17:50:24 EDT 2003


On Fri, 18 Jul 2003 13:26:59 +0200, =?ISO-8859-1?Q?Gerhard_H=E4ring?= <gh at ghaering.de> wrote:

>Martin P wrote:
>> Hello,
>> 
>> for a short exercise-program I have to read the lines in an ASCII-file.
>> 
>> But every line has a new-line-feed ("\n") at the end of the line. (The
>> ASCII-file was made with Windows Notepad).
>> Is this only because I used Windows (and Notepad)?
>
>Yes.
>
>> And... is there any special method to read lines from a file without "\n"?
>
>Most of the time, I want to strip any trailing whitespace and do 
Nit:                                   ^^^^^^^^
>something like:
>
>f = file("/path/to/my/file")
>for line in f:
>     line = line.strip()
                 .rstrip()
                 ^^^^^^^^^      
>     # ...
>
>Unlike Karl's method, this scales to larger files without needing to 
>read the whole file into memory.
>
>Files are iterable since Python 2.2.
>

Regards,
Bengt Richter




More information about the Python-list mailing list