<> and chomp

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

<> and chomp

William Allison
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.
Thanks,
Will

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: <> and chomp

David Rock
* William Allison <[hidden email]> [2006-09-14 12:14]:
> 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.

fileinput() is similar to <>
http://docs.python.org/lib/module-fileinput.html

rstrip() is similar to chomp
http://docs.python.org/lib/string-methods.html#l2h-201

These aren't exact matches, but they are close.

--
David Rock
[hidden email]
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: <> and chomp

Kent Johnson
In reply to this post by William Allison
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

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: <> and chomp

Dave Kuhlman
In reply to this post by William Allison
On Thu, Sep 14, 2006 at 12:14:27PM -0400, 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.
> Thanks,
> Will

Consider:

    infile = open('infilename.txt', 'r')
    for line in infile:
        line = line.rstrip('\n')
        o
        o
        o
    infile.close()

A few notes:

- See http://docs.python.org/lib/built-in-funcs.html for more on
  built-in functions and open() in particular.

- See http://docs.python.org/lib/string-methods.html for more on
  string operations and rstrip() in particular.

- rstrip() with no arguments strips all whitespace on the right.

- A file object (returned by the open() function) is an iterator; it
  obeys the iterator protocol.  That's why you can use it in the
  "for" statement above.

Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor