read lines without the line break character at the end?
Wai Yip Tung
tungwaiyip at yahoo.com
Thu Sep 2 19:51:41 EDT 2004
More information about the Python-list mailing list
Thu Sep 2 19:51:41 EDT 2004
- Previous message (by thread): BayPIGgies: September 9, 7:30pm
- Next message (by thread): read lines without the line break character at the end?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Thanks, I like the rstrip() syntax. Coming from Java background. I made too many mistakes forgetting to strip the '\n' before using the string. > "Wai Yip Tung" <tungwaiyip at yahoo.com> writes: > >> When I do >> >> for line in fp: >> >> the line string usually has a '\n' at the end. In many cases I don't >> want the line break character. I can trim it using >> >> if line.endswith('\n'): line = line[:-1] >> >> Given I do it so frequently I wonder if there is some builtin way to >> say I don't wnt the line break character? > > No. line.rstrip('\n') is a shorter way of trimming the newline > character, though. > > I guess adding > > def without_newlines(thing): > for line in thing: > yield line.rstrip('\n') > > to your snippet collection will do. > > Cheers, > mwh >
- Previous message (by thread): BayPIGgies: September 9, 7:30pm
- Next message (by thread): read lines without the line break character at the end?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list