Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80554

Re: What is rstrip() in python?

Newsgroups comp.lang.python
Date 2014-11-11 11:53 -0800
References <c939252e-673b-4cd2-9399-76753a7dd3f7@googlegroups.com>
Message-ID <bdee0dc6-bf42-4da2-a411-6d0d8cf1dd39@googlegroups.com> (permalink)
Subject Re: What is rstrip() in python?
From Will Acheson <willyach07@gmail.com>

Show all headers | View raw


On Sunday, November 9, 2014 6:12:24 AM UTC-5, satish...@gmail.com wrote:
> What is rstrip() in python?
> 
> What does it do in the following piece of code?
> 
> import sqlite3
> conn = sqlite3.connect('dbase1')
> curs = conn.cursor()
> 
> file = open('data.txt')
> rows = [line.rstrip().split(',') for line in file]

rstrip() removes whitespace, newline characters, tab characters, and carrige return characters (\n \t \r respectively) on the tail of a string.  

Or it can be used with an input parameter to remove all instances of that parameter from the tail of a string.

ex:
 stringy = "i am helpful  \t\t\t\t\n\n      "
 stringy = stringy.rstrip()
 print stringy

stdout: "i am helpful"

or:
 stringy = "my favorite number is 8000000000000000000000"
 stringy = stringy.rstrip('0')
 print stringy

stdout: "my favorite number is 8"



pretty simple method, helpful for parsing out formatting characters from scraped content from webpages.
https://docs.python.org/2/library/stdtypes.html?highlight=rstrip#str.rstrip

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

What is rstrip() in python? satishmlmlml@gmail.com - 2014-11-09 03:11 -0800
  Re: What is rstrip() in python? Chris Angelico <rosuav@gmail.com> - 2014-11-09 22:28 +1100
  Re: What is rstrip() in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-09 23:57 +1100
  Re: What is rstrip() in python? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-09 14:17 +0000
  Re: What is rstrip() in python? Terry Reedy <tjreedy@udel.edu> - 2014-11-09 12:46 -0500
  Re: What is rstrip() in python? Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-09 14:16 -0500
  Re: What is rstrip() in python? Will Acheson <willyach07@gmail.com> - 2014-11-11 11:53 -0800

csiph-web