newby question: Splitting a string - separator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Liesner

    #1

    newby question: Splitting a string - separator

    Hi all,

    i am having a textfile which contains a single string with names.
    I want to split this string into its records an put them into a list.
    In "normal" cases i would do something like:
    [color=blue]
    > #!/usr/bin/python
    > inp = open("file")
    > data = inp.read()
    > names = data.split()
    > inp.close()[/color]

    The problem is, that the names contain spaces an the records are also
    just seprarated by spaces. The only thing i can rely on, ist that the
    recordseparator is always more than a single whitespace.

    I thought of something like defining the separator for split() by using
    a regex for "more than one whitespace". RegEx for whitespace is \s, but
    what would i use for "more than one"? \s+?

    TIA,
    Tom
  • Michael Spencer

    #2
    Re: newby question: Splitting a string - separator

    Thomas Liesner wrote:[color=blue]
    > Hi all,
    >
    > i am having a textfile which contains a single string with names.
    > I want to split this string into its records an put them into a list.
    > In "normal" cases i would do something like:
    >[color=green]
    >> #!/usr/bin/python
    >> inp = open("file")
    >> data = inp.read()
    >> names = data.split()
    >> inp.close()[/color]
    >
    > The problem is, that the names contain spaces an the records are also
    > just seprarated by spaces. The only thing i can rely on, ist that the
    > recordseparator is always more than a single whitespace.
    >
    > I thought of something like defining the separator for split() by using
    > a regex for "more than one whitespace". RegEx for whitespace is \s, but
    > what would i use for "more than one"? \s+?
    >
    > TIA,
    > Tom[/color]
    \s+ gives one or more, you need \s{2,} for two or more:
    [color=blue][color=green][color=darkred]
    >>> import re
    >>> re.split("\s{2, }","Guido van Rossum Tim Peters Thomas Liesner")[/color][/color][/color]
    ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner'][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Michael

    Comment

    • Noah

      #3
      Re: newby question: Splitting a string - separator


      Thomas Liesner wrote:[color=blue]
      > ...
      > The only thing i can rely on, ist that the
      > recordseparator is always more than a single whitespace.
      >
      > I thought of something like defining the separator for split() by using
      > a regex for "more than one whitespace". RegEx for whitespace is \s, but
      > what would i use for "more than one"? \s+?[/color]

      For your split regex you could say
      "\s\s+"
      or
      "\s{2,}"

      This should work for you:
      YOUR_SPLIT_LIST = re.split("\s{2, }", YOUR_STRING)

      Yours,
      Noah

      Comment

      • Jim

        #4
        Re: newby question: Splitting a string - separator

        Hi Tom,
        [color=blue]
        > a regex for "more than one whitespace". RegEx for whitespace is \s, but
        > what would i use for "more than one"? \s+?[/color]

        For more than one, I'd use

        \s\s+

        -Jim

        Comment

        • James Stroud

          #5
          Re: newby question: Splitting a string - separator

          Thomas Liesner wrote:[color=blue]
          > Hi all,
          >
          > i am having a textfile which contains a single string with names.
          > I want to split this string into its records an put them into a list.
          > In "normal" cases i would do something like:
          >
          >[color=green]
          >>#!/usr/bin/python
          >>inp = open("file")
          >>data = inp.read()
          >>names = data.split()
          >>inp.close()[/color]
          >
          >
          > The problem is, that the names contain spaces an the records are also
          > just seprarated by spaces. The only thing i can rely on, ist that the
          > recordseparator is always more than a single whitespace.
          >
          > I thought of something like defining the separator for split() by using
          > a regex for "more than one whitespace". RegEx for whitespace is \s, but
          > what would i use for "more than one"? \s+?
          >
          > TIA,
          > Tom[/color]

          The one I like best goes like this:

          py> data = "Guido van Rossum Tim Peters Thomas Liesner"
          py> names = [n for n in data.split() if n]
          py> names
          ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']

          I think it is theoretically faster (and more pythonic) than using regexes.

          James

          Comment

          • Kent Johnson

            #6
            Re: newby question: Splitting a string - separator

            James Stroud wrote:[color=blue]
            > The one I like best goes like this:
            >
            > py> data = "Guido van Rossum Tim Peters Thomas Liesner"
            > py> names = [n for n in data.split() if n]
            > py> names
            > ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
            >
            > I think it is theoretically faster (and more pythonic) than using regexes.[/color]

            Unfortunately it gives the wrong result.

            Kent

            Comment

            • Tim Peters

              #7
              Re: newby question: Splitting a string - separator

              [James Stroud][color=blue][color=green]
              >> The one I like best goes like this:
              >>
              >> py> data = "Guido van Rossum Tim Peters Thomas Liesner"
              >> py> names = [n for n in data.split() if n]
              >> py> names
              >> ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
              >>
              >> I think it is theoretically faster (and more pythonic) than using regexes.[/color][/color]

              [Kent Johnson][color=blue]
              > Unfortunately it gives the wrong result.[/color]

              Still, it gets extra points for being such a pleasing example ;-)

              Comment

              • bonono@gmail.com

                #8
                Re: newby question: Splitting a string - separator


                Thomas Liesner wrote:[color=blue]
                > Hi all,
                >
                > i am having a textfile which contains a single string with names.
                > I want to split this string into its records an put them into a list.
                > In "normal" cases i would do something like:
                >[color=green]
                > > #!/usr/bin/python
                > > inp = open("file")
                > > data = inp.read()
                > > names = data.split()
                > > inp.close()[/color]
                >
                > The problem is, that the names contain spaces an the records are also
                > just seprarated by spaces. The only thing i can rely on, ist that the
                > recordseparator is always more than a single whitespace.
                >
                > I thought of something like defining the separator for split() by using
                > a regex for "more than one whitespace". RegEx for whitespace is \s, but
                > what would i use for "more than one"? \s+?
                >[/color]
                Can I just use "two space" as the seperator ?

                [ x.strip() for x in data.split(" ") ]

                Comment

                • James Stroud

                  #9
                  Re: newby question: Splitting a string - separator

                  Kent Johnson wrote:[color=blue]
                  > James Stroud wrote:
                  >[color=green]
                  >> The one I like best goes like this:
                  >>
                  >> py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                  >> py> names = [n for n in data.split() if n]
                  >> py> names
                  >> ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
                  >>
                  >> I think it is theoretically faster (and more pythonic) than using
                  >> regexes.[/color]
                  >
                  >
                  > Unfortunately it gives the wrong result.
                  >
                  > Kent[/color]

                  Just an example. Here is the "correct version":


                  names = [n for n in data.split(" ") if n]

                  James

                  Comment

                  • Michael Spencer

                    #10
                    Re: newby question: Splitting a string - separator

                    bonono@gmail.co m wrote:[color=blue]
                    > Thomas Liesner wrote:[color=green]
                    >> Hi all,
                    >>
                    >> i am having a textfile which contains a single string with names.
                    >> I want to split this string into its records an put them into a list.
                    >> In "normal" cases i would do something like:
                    >>[color=darkred]
                    >>> #!/usr/bin/python
                    >>> inp = open("file")
                    >>> data = inp.read()
                    >>> names = data.split()
                    >>> inp.close()[/color]
                    >> The problem is, that the names contain spaces an the records are also
                    >> just seprarated by spaces. The only thing i can rely on, ist that the
                    >> recordseparator is always more than a single whitespace.
                    >>
                    >> I thought of something like defining the separator for split() by using
                    >> a regex for "more than one whitespace". RegEx for whitespace is \s, but
                    >> what would i use for "more than one"? \s+?
                    >>[/color]
                    > Can I just use "two space" as the seperator ?
                    >
                    > [ x.strip() for x in data.split(" ") ]
                    >[/color]
                    If you like, but it will create dummy entries if there are more than two spaces:
                    [color=blue][color=green][color=darkred]
                    >>> data = "Guido van Rossum Tim Peters Thomas Liesner"
                    >>> [ x.strip() for x in data.split(" ") ][/color][/color][/color]
                    ['Guido van Rossum', 'Tim Peters', '', 'Thomas Liesner']

                    You could add a condition to the listcomp:
                    [color=blue][color=green][color=darkred]
                    >>> [name.strip() for name in data.split(" ") if name][/color][/color][/color]
                    ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner']

                    but what if there is some other whitespace character?
                    [color=blue][color=green][color=darkred]
                    >>> data = "Guido van Rossum Tim Peters \t Thomas Liesner"
                    >>> [name.strip() for name in data.split(" ") if name][/color][/color][/color]
                    ['Guido van Rossum', 'Tim Peters', '', 'Thomas Liesner'][color=blue][color=green][color=darkred]
                    >>>[/color][/color][/color]

                    perhaps a smarter condition?
                    [color=blue][color=green][color=darkred]
                    >>> [name.strip() for name in data.split(" ") if name.strip(" \t")][/color][/color][/color]
                    ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner']

                    but this is beginning to feel like hard work.


                    I think this is a case where it's not worth the effort to try to avoid the regexp
                    [color=blue][color=green][color=darkred]
                    >>> import re
                    >>> re.split("\s{2, }",data)[/color][/color][/color]
                    ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner'][color=blue][color=green][color=darkred]
                    >>>[/color][/color][/color]

                    Michael


                    Comment

                    • Steven D'Aprano

                      #11
                      Re: newby question: Splitting a string - separator

                      On Fri, 09 Dec 2005 18:02:02 -0800, James Stroud wrote:
                      [color=blue]
                      > Thomas Liesner wrote:[color=green]
                      >> Hi all,
                      >>
                      >> i am having a textfile which contains a single string with names.
                      >> I want to split this string into its records an put them into a list.
                      >> In "normal" cases i would do something like:
                      >>
                      >>[color=darkred]
                      >>>#!/usr/bin/python
                      >>>inp = open("file")
                      >>>data = inp.read()
                      >>>names = data.split()
                      >>>inp.close( )[/color]
                      >>
                      >>
                      >> The problem is, that the names contain spaces an the records are also
                      >> just seprarated by spaces. The only thing i can rely on, ist that the
                      >> recordseparator is always more than a single whitespace.
                      >>
                      >> I thought of something like defining the separator for split() by using
                      >> a regex for "more than one whitespace". RegEx for whitespace is \s, but
                      >> what would i use for "more than one"? \s+?
                      >>
                      >> TIA,
                      >> Tom[/color]
                      >
                      > The one I like best goes like this:
                      >
                      > py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                      > py> names = [n for n in data.split() if n]
                      > py> names
                      > ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
                      >
                      > I think it is theoretically faster (and more pythonic) than using regexes.[/color]


                      Yes, but the correct result would be:

                      ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner']

                      Your code is short, elegant but wrong.

                      It could also be shorter and more elegant:

                      # your version
                      py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                      py> [n for n in data.split() if n]
                      ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']

                      # my version
                      py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                      py> data.split()
                      ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']

                      The "if n" in the list comp is superfluous, and without that, the whole
                      list comp is unnecessary.



                      --
                      Steven.

                      Comment

                      • James Stroud

                        #12
                        Re: newby question: Splitting a string - separator

                        Steven D'Aprano wrote:[color=blue]
                        > On Fri, 09 Dec 2005 18:02:02 -0800, James Stroud wrote:
                        >
                        >[color=green]
                        >>Thomas Liesner wrote:
                        >>[color=darkred]
                        >>>Hi all,
                        >>>
                        >>>i am having a textfile which contains a single string with names.
                        >>>I want to split this string into its records an put them into a list.
                        >>>In "normal" cases i would do something like:
                        >>>
                        >>>
                        >>>
                        >>>>#!/usr/bin/python
                        >>>>inp = open("file")
                        >>>>data = inp.read()
                        >>>>names = data.split()
                        >>>>inp.close ()
                        >>>
                        >>>
                        >>>The problem is, that the names contain spaces an the records are also
                        >>>just seprarated by spaces. The only thing i can rely on, ist that the
                        >>>recordsepara tor is always more than a single whitespace.
                        >>>
                        >>>I thought of something like defining the separator for split() by using
                        >>> a regex for "more than one whitespace". RegEx for whitespace is \s, but
                        >>>what would i use for "more than one"? \s+?
                        >>>
                        >>>TIA,
                        >>>Tom[/color]
                        >>
                        >>The one I like best goes like this:
                        >>
                        >>py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                        >>py> names = [n for n in data.split() if n]
                        >>py> names
                        >>['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
                        >>
                        >>I think it is theoretically faster (and more pythonic) than using regexes.[/color]
                        >
                        >
                        >
                        > Yes, but the correct result would be:
                        >
                        > ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner']
                        >
                        > Your code is short, elegant but wrong.
                        >
                        > It could also be shorter and more elegant:
                        >
                        > # your version
                        > py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                        > py> [n for n in data.split() if n]
                        > ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
                        >
                        > # my version
                        > py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                        > py> data.split()
                        > ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
                        >
                        > The "if n" in the list comp is superfluous, and without that, the whole
                        > list comp is unnecessary.
                        >
                        >
                        >[/color]
                        see my post from 1 hr before this one.

                        Comment

                        • Tim Roberts

                          #13
                          Re: newby question: Splitting a string - separator

                          James Stroud <jstroud@mbi.uc la.edu> wrote:[color=blue]
                          >
                          >The one I like best goes like this:
                          >
                          >py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                          >py> names = [n for n in data.split() if n]
                          >py> names
                          >['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
                          >
                          >I think it is theoretically faster (and more pythonic) than using regexes.[/color]

                          But it is slower than this, which produces EXACTLY the same (incorrect)
                          result:

                          data = "Guido van Rossum Tim Peters Thomas Liesner"
                          names = data.split()
                          --
                          - Tim Roberts, timr@probo.com
                          Providenza & Boekelheide, Inc.

                          Comment

                          • Fredrik Lundh

                            #14
                            Re: newby question: Splitting a string - separator

                            James Stroud wrote:
                            [color=blue][color=green][color=darkred]
                            > >> py> data = "Guido van Rossum Tim Peters Thomas Liesner"
                            > >> py> names = [n for n in data.split() if n]
                            > >> py> names
                            > >> ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
                            > >>
                            > >> I think it is theoretically faster (and more pythonic) than using
                            > >> regexes.[/color]
                            > >
                            > > Unfortunately it gives the wrong result.[/color]
                            >
                            > Just an example. Here is the "correct version":
                            >
                            > names = [n for n in data.split(" ") if n][/color]

                            where "correct" is "still wrong", and "theoretica lly faster" means "slightly
                            slower" (at least if fix your version, and precompile the pattern).

                            </F>



                            Comment

                            Working...