values within quotes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guyon Morée

    values within quotes

    Hi all,

    I looked on the web for this, but I couldn't find it.

    I give my program commands like this: GET MODLIST

    This is very easy to parse, I can just use .split() to because it has only
    spaces to seperate the different parts of the command. Now I want, for
    example, the following command: GET FILE C:\my docs\my file.txt. Obviously I
    cannot use .split() for this one, because it uses spaces in the filename.
    Common pc knowledge tells me that I should put the 'space-value' witin
    quotes like: GET FILE "C:\my docs\my file.txt".

    So the big question is: What is the best way to parse such a string?

    kind regards,

    Guyon


  • Martin Franklin

    #2
    Re: values within quotes

    Guyon Morée wrote:[color=blue]
    > Hi all,
    >
    > I looked on the web for this, but I couldn't find it.
    >
    > I give my program commands like this: GET MODLIST
    >
    > This is very easy to parse, I can just use .split() to because it has only
    > spaces to seperate the different parts of the command. Now I want, for
    > example, the following command: GET FILE C:\my docs\my file.txt. Obviously I
    > cannot use .split() for this one, because it uses spaces in the filename.
    > Common pc knowledge tells me that I should put the 'space-value' witin
    > quotes like: GET FILE "C:\my docs\my file.txt".
    >
    > So the big question is: What is the best way to parse such a string?
    >
    > kind regards,
    >
    > Guyon
    >
    > [/color]

    Look at the csv module in the standard library.

    from pydoc csv :-


    Help on module csv:

    NAME
    csv - CSV parsing and writing.

    FILE
    /usr/lib/python2.3/csv.py

    DESCRIPTION
    This module provides classes that assist in the reading and writing
    of Comma Separated Value (CSV) files, and implements the interface
    described by PEP 305. Although many CSV files are simple to parse,
    the format is not formally defined by a stable specification and
    is subtle enough that parsing lines of a CSV file with something
    like line.split(",") is bound to fail. The module supports three
    basic APIs: reading, writing, and registration of dialects.



    HTH
    Martin


    Comment

    • F. Petitjean

      #3
      Re: values within quotes

      On Mon, 22 Mar 2004 11:18:08 +0100, Guyon Morée <gumuz@looze.ne t> wrote:[color=blue]
      > Hi all,
      >
      > Now I want, for
      > example, the following command: GET FILE C:\my docs\my file.txt. Obviously I
      > cannot use .split() for this one, because it uses spaces in the filename.
      > Common pc knowledge tells me that I should put the 'space-value' witin
      > quotes like: GET FILE "C:\my docs\my file.txt".
      >
      > So the big question is: What is the best way to parse such a string?[/color]

      distutils is a standard package with some gems :
      from distutils.util import split_quoted
      help(split_quot ed)
      s = 'GET FILE "C:\\my docs\\my file.txt"'
      split_quoted(s)
      ['GET', 'FILE', 'C:\\my docs\\my file.txt']

      and don't fall in the backslash trap, use forward slashes whenever possible.[color=blue]
      >
      > kind regards,
      >
      > Guyon
      >
      >[/color]

      Comment

      • Mel Wilson

        #4
        Re: values within quotes

        In article <405ebd60$0$268 $4d4ebb8e@news. nl.uu.net>,
        "Guyon Morée" <gumuz@looze.ne t> wrote:[color=blue]
        >I give my program commands like this: GET MODLIST
        >
        >This is very easy to parse, I can just use .split() to because it has only
        >spaces to seperate the different parts of the command. Now I want, for
        >example, the following command: GET FILE C:\my docs\my file.txt. Obviously I
        >cannot use .split() for this one, because it uses spaces in the filename.
        >Common pc knowledge tells me that I should put the 'space-value' witin
        >quotes like: GET FILE "C:\my docs\my file.txt".
        >
        >So the big question is: What is the best way to parse such a string?[/color]

        Best has probably been posted, but for the record another way is
        [color=blue][color=green][color=darkred]
        >>> command = r"GET FILE C:\my docs\my file.txt" # simulate a read from the world outside
        >>> command.split (None, 2)[/color][/color][/color]
        ['GET', 'FILE', 'C:\\my docs\\my file.txt']


        Regards. Mel.

        Comment

        Working...