Newbie needs help with regex strings
Michael Spencer
mahs at telcopartners.com
Wed Dec 14 14:31:06 EST 2005
Dennis Benzinger wrote:
> Christopher Subich schrieb:
>> Paul McGuire wrote:
>>
>> [...]
>> For the example listed, pyparsing is even overkill; the OP should
>> probably use the csv module.
>
> But the OP wants to parse lines with key=value pairs, not simply lines
> with comma separated values. Using the csv module will just separate the
> key=value pairs and you would still have to take them apart.
>
> Bye,
> Dennis
that, and csv.reader has another problem with this task:
>>> csv.reader(["Pie=peach,quantity=2,ingredients='peaches,powdered sugar'"],
quotechar = "'").next()
['Pie=peach', 'quantity=2', "ingredients='peaches", "powdered sugar'"]
i.e., it doesn't allow separators within fields unless either the *whole* field
is quoted:
>>> csv.reader(["Pie=peach,quantity=2,'ingredients=peaches,powdered sugar'"],
quotechar = "'").next()
['Pie=peach', 'quantity=2', 'ingredients=peaches,powdered sugar']
>>>
or the separator is escaped:
>>> csv.reader(["Pie=peach,quantity=2,ingredients='peaches\,powdered sugar'"],
quotechar = "'", escapechar = "\\").next()
['Pie=peach', 'quantity=2', "ingredients='peaches,powdered sugar'"]
>>>
Michael
More information about the Python-list
mailing list