[Tutor] <> and chomp
Kent Johnson
kent37 at tds.net
Thu Sep 14 18:34:37 CEST 2006
William Allison wrote:
> Hi,
> I'm new to programming and started with Perl but have been reading a lot
> of good things about Python. Thought I would switch before I have too
> much time invested in Perl.
> Anyway, my question is, is there something in Python similar to the
> diamond operator and chomp from Perl? I'm trying to read in from a file
> line by line and get rid of the '\n' at the end of each line.
for line in open('somefile.txt'):
line = line.rstrip('\n')
# do something with line
You could also use line = line.rstrip() which removes all trailing white
space including \n, or line.strip() which removes leading and trailing
white space.
Kent
More information about the Tutor
mailing list