Stripping whitespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ryan k

    Stripping whitespace

    Hello. I have a string like 'LNAME
    PASTA ZONE'. I want to create a list of those words and
    basically replace all the whitespace between them with one space so i
    could just do lala.split(). Thank you!

    Ryan Kaskel
  • Marc 'BlackJack' Rintsch

    #2
    Re: Stripping whitespace

    On Wed, 23 Jan 2008 10:50:02 -0800, ryan k wrote:
    Hello. I have a string like 'LNAME
    PASTA ZONE'. I want to create a list of those words and
    basically replace all the whitespace between them with one space so i
    could just do lala.split(). Thank you!
    You *can* just do ``lala.split()` `:

    In [97]: lala = 'LNAME PASTA ZONE'

    In [98]: lala.split()
    Out[98]: ['LNAME', 'PASTA', 'ZONE']

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Paul Rubin

      #3
      Re: Stripping whitespace

      ryan k <ryan@ryankaske l.comwrites:
      Hello. I have a string like 'LNAME
      PASTA ZONE'. I want to create a list of those words and
      basically replace all the whitespace between them with one space so i
      could just do lala.split(). Thank you!
      import re
      s = 'LNAME PASTA ZONE'
      re.split('\s+', s)

      Comment

      • John Machin

        #4
        Re: Stripping whitespace

        On Jan 24, 5:50 am, ryan k <r...@ryankaske l.comwrote:
        Hello. I have a string like 'LNAME
        PASTA ZONE'. I want to create a list of those words and
        basically replace all the whitespace between them with one space so i
        could just do lala.split(). Thank you!
        >
        Ryan Kaskel
        So when you go to the Python interactive prompt and type firstly
        lala = 'LNAME PASTA ZONE'
        and then
        lala.split()
        what do you see, and what more do you need to meet your requirements?

        Comment

        • James Matthews

          #5
          Re: Stripping whitespace

          Using the split method is the easiest!

          On 23 Jan 2008 19:04:38 GMT, Marc 'BlackJack' Rintsch <bj_666@gmx.net wrote:
          On Wed, 23 Jan 2008 10:50:02 -0800, ryan k wrote:
          >
          Hello. I have a string like 'LNAME
          PASTA ZONE'. I want to create a list of those words and
          basically replace all the whitespace between them with one space so i
          could just do lala.split(). Thank you!
          >
          You *can* just do ``lala.split()` `:
          >
          In [97]: lala = 'LNAME PASTA ZONE'
          >
          In [98]: lala.split()
          Out[98]: ['LNAME', 'PASTA', 'ZONE']
          >
          Ciao,
          Marc 'BlackJack' Rintsch
          >
          --

          >


          --



          Comment

          • ryan k

            #6
            Re: Stripping whitespace

            On Jan 23, 2:04 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
            On Wed, 23 Jan 2008 10:50:02 -0800, ryan k wrote:
            Hello. I have a string like 'LNAME
            PASTA ZONE'. I want to create a list of those words and
            basically replace all the whitespace between them with one space so i
            could just do lala.split(). Thank you!
            >
            You *can* just do ``lala.split()` `:
            Indeed you can thanks!
            >
            In [97]: lala = 'LNAME PASTA ZONE'
            >
            In [98]: lala.split()
            Out[98]: ['LNAME', 'PASTA', 'ZONE']
            >
            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            • John Machin

              #7
              Re: Stripping whitespace

              On Jan 24, 6:05 am, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
              ryan k <r...@ryankaske l.comwrites:
              Hello. I have a string like 'LNAME
              PASTA ZONE'. I want to create a list of those words and
              basically replace all the whitespace between them with one space so i
              could just do lala.split(). Thank you!
              >
              import re
              s = 'LNAME PASTA ZONE'
              re.split('\s+', s)
              That is (a) excessive for the OP's problem as stated and (b) unlike
              str.split will cause him to cut you out of his will if his problem
              turns out to include leading/trailing whitespace:
              >>lala = ' LNAME PASTA ZONE '
              >>import re
              >>re.split(r'\s +', lala)
              ['', 'LNAME', 'PASTA', 'ZONE', '']
              >>lala.split( )
              ['LNAME', 'PASTA', 'ZONE']
              >>>

              Comment

              • John Machin

                #8
                Re: Stripping whitespace

                On Jan 24, 6:57 am, ryan k <r...@ryankaske l.comwrote:
                So yea i will just have to count dashes.
                Read my lips: *you* counting dashes is dumb. Writing your code so that
                *code* is counting dashes each time it opens the file is smart.

                Comment

                • John Machin

                  #9
                  Re: Stripping whitespace

                  On Jan 24, 7:23 am, ryan k <r...@ryankaske l.comwrote:
                  On Jan 23, 3:02 pm, John Machin <sjmac...@lexic on.netwrote:
                  >
                  On Jan 24, 6:57 am, ryan k <r...@ryankaske l.comwrote:
                  >
                  So yea i will just have to count dashes.
                  >
                  Read my lips: *you* counting dashes is dumb. Writing your code so that
                  *code* is counting dashes each time it opens the file is smart.
                  >
                  Okay it's almost working ...
                  >
                  new parser function:
                  >
                  def _load_table(sel f):
                  counter = 0
                  for line in self.table_fd:
                  # Skip the second line
                  The above comment is a nonsense.
                  if counter == 0:
                  # This line contains the columns, parse it
                  column_list = line.split()
                  In generality, you would have to allow for the headings to contain
                  spaces as well -- this means *saving* a reference to the heading line
                  and splitting it *after* you've processed the line with the dashes.
                  elif counter == 1:
                  # These are the dashes
                  line_l = line.split()
                  column_width = [len(i) for i in line_l]
                  Whoops.
                  column_width = [len(i) + 1 for i in line_l]
                  print column_width
                  else:
                  # This is a row, parse it
                  marker = 0
                  row_vals = []
                  for col in column_width:
                  start = sum(column_widt h[:marker])
                  finish = sum(column_widt h[:marker+1])
                  print line[start:finish].strip()
                  If you had printed just line[start:finish], it would have been obvious
                  what the problem was. See below for an even better suggestion.
                  row_vals.append (line[start:finish].strip())
                  marker += 1
                  Using sum is a tad ugly. Here's an alternative:

                  row_vals = []
                  start = 0
                  for width in column_width:
                  finish = start + width
                  #DEBUG# print repr(line[start:finish].replace(' ', '~'))
                  row_vals.append (line[start:finish].strip())
                  start = finish
                  self.rows.appen d(Row(column_li st, row_vals))
                  counter += 1
                  >
                  Something obvious you can see wrong with my start finish code?
                  See above.

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: Stripping whitespace

                    On Wed, 23 Jan 2008 11:05:01 -0800, Paul Rubin wrote:
                    ryan k <ryan@ryankaske l.comwrites:
                    >Hello. I have a string like 'LNAME
                    >PASTA ZONE'. I want to create a list of those words and
                    >basically replace all the whitespace between them with one space so i
                    >could just do lala.split(). Thank you!
                    >
                    import re
                    s = 'LNAME PASTA ZONE'
                    re.split('\s+', s)
                    Please tell me you're making fun of the poor newbie and didn't mean to
                    seriously suggest using a regex merely to split on whitespace?
                    >>import timeit
                    >>timeit.Timer( "s.split()" , "s = 'one two three four'").repeat( )
                    [1.4074358940124 512, 1.3505148887634 277, 1.3469438552856 445]
                    >>timeit.Timer( "re.split('\s+' , s)", "import re;s = 'one two
                    three four'").repeat( )
                    [7.9205508232116 699, 7.8833441734313 965, 7.9301259517669 678]





                    --
                    Steven

                    Comment

                    • ryan k

                      #11
                      Re: Stripping whitespace

                      On Jan 23, 5:37 pm, Steven D'Aprano <st...@REMOVE-THIS-
                      cybersource.com .auwrote:
                      On Wed, 23 Jan 2008 11:05:01 -0800, Paul Rubin wrote:
                      ryan k <r...@ryankaske l.comwrites:
                      Hello. I have a string like 'LNAME
                      PASTA ZONE'. I want to create a list of those words and
                      basically replace all the whitespace between them with one space so i
                      could just do lala.split(). Thank you!
                      >
                      import re
                      s = 'LNAME PASTA ZONE'
                      re.split('\s+', s)
                      >
                      Please tell me you're making fun of the poor newbie and didn't mean to
                      seriously suggest using a regex merely to split on whitespace?
                      >
                      >import timeit
                      >timeit.Timer(" s.split()", "s = 'one two three four'").repeat( )
                      >
                      [1.4074358940124 512, 1.3505148887634 277, 1.3469438552856 445]>>timeit.Timer( "re.split('\s+' , s)", "import re;s = 'one two
                      >
                      three four'").repeat( )
                      [7.9205508232116 699, 7.8833441734313 965, 7.9301259517669 678]
                      >
                      --
                      Steven
                      The main topic is not an issue anymore.

                      Comment

                      • ryan k

                        #12
                        Re: Stripping whitespace

                        On Jan 23, 5:37 pm, Steven D'Aprano <st...@REMOVE-THIS-
                        cybersource.com .auwrote:
                        On Wed, 23 Jan 2008 11:05:01 -0800, Paul Rubin wrote:
                        ryan k <r...@ryankaske l.comwrites:
                        Hello. I have a string like 'LNAME
                        PASTA ZONE'. I want to create a list of those words and
                        basically replace all the whitespace between them with one space so i
                        could just do lala.split(). Thank you!
                        >
                        import re
                        s = 'LNAME PASTA ZONE'
                        re.split('\s+', s)
                        >
                        Please tell me you're making fun of the poor newbie and didn't mean to
                        seriously suggest using a regex merely to split on whitespace?
                        >
                        >import timeit
                        >timeit.Timer(" s.split()", "s = 'one two three four'").repeat( )
                        >
                        [1.4074358940124 512, 1.3505148887634 277, 1.3469438552856 445]>>timeit.Timer( "re.split('\s+' , s)", "import re;s = 'one two
                        >
                        three four'").repeat( )
                        [7.9205508232116 699, 7.8833441734313 965, 7.9301259517669 678]
                        >
                        --
                        Steven
                        Much thanks to Machin for helping with the parsing job. Steven
                        D'Aprano, you are a prick.

                        Comment

                        • John Machin

                          #13
                          Re: Stripping whitespace

                          On Jan 24, 9:50 am, ryan k <r...@ryankaske l.comwrote:
                          Steven D'Aprano, you are a prick.
                          And your reasons for coming to that stridently expressed conclusion
                          after reading a posting that was *not* addressed to you are .....?

                          Comment

                          • ryan k

                            #14
                            Re: Stripping whitespace

                            On Jan 23, 6:30 pm, John Machin <sjmac...@lexic on.netwrote:
                            On Jan 24, 9:50 am, ryan k <r...@ryankaske l.comwrote:
                            >
                            Steven D'Aprano, you are a prick.
                            >
                            And your reasons for coming to that stridently expressed conclusion
                            after reading a posting that was *not* addressed to you are .....?
                            Because his tone is extremely condescending and quite frankly
                            annoying. From this post to others, his terse remarks scar this
                            community and fade its atmosphere of friendliness.

                            Comment

                            • cokofreedom@gmail.com

                              #15
                              Re: Stripping whitespace

                              On Jan 24, 8:21 am, ryan k <r...@ryankaske l.comwrote:
                              On Jan 23, 6:30 pm, John Machin <sjmac...@lexic on.netwrote:
                              >
                              On Jan 24, 9:50 am, ryan k <r...@ryankaske l.comwrote:
                              >
                              Steven D'Aprano, you are a prick.
                              >
                              And your reasons for coming to that stridently expressed conclusion
                              after reading a posting that was *not* addressed to you are .....?
                              >
                              Because his tone is extremely condescending and quite frankly
                              annoying. From this post to others, his terse remarks scar this
                              community and fade its atmosphere of friendliness.
                              And your response was any better because...?

                              Comment

                              Working...