By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

values within quotes

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Guyon Morée wrote:
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



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
Jul 18 '05 #2

P: n/a
On Mon, 22 Mar 2004 11:18:08 +0100, Guyon Morée <gu***@looze.net> wrote:
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?
distutils is a standard package with some gems :
from distutils.util import split_quoted
help(split_quoted)
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.
kind regards,

Guyon

Jul 18 '05 #3

P: n/a
In article <40*********************@news.nl.uu.net>,
"Guyon Morée" <gu***@looze.net> wrote:
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?


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

['GET', 'FILE', 'C:\\my docs\\my file.txt']
Regards. Mel.
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.