From: Padraig Brady (padraig.brady at domain corvil.com)
Date: Wed 11 Sep 2002 - 14:30:47 IST
Padraig Brady wrote:
> Waider wrote:
>
>> Padraig Brady wrote:
>> |
>> | What I have in python now is:
>> | fields = re.split('[ ]+|"([^"]*)"', record)
>> | And this is OK except there are empty and
>> | null fields returned. I'm sure there is
>> | a more elegant re to acheive this?
>> |
>>
>> Here's the canonical Perl solution:
>> http://www.perldoc.com/perl5.6/faq/perlfaq4.html (search for split)
>
> Ouch, kinda scary. python has (?:) support also.
>
>> Personally, when faced with this sort of task, I just break it down
>> rather than trying to accomplish it in a single clever step. Usually I
>> get the code written faster.
>
> Probably better.
I took the KISS approach.
FYI, here's the python code.
Padraig.
--------------------------
#!/usr/bin/env python
import re, fileinput
reFieldSplitter = re.compile('[^ "]+|"[^"]+"') #unquoted|quoted
def getFields(line): #without \n
fields = reFieldSplitter.findall(line) #find fields
fields = map(lambda field: field.replace('"', ''), fields) #remove quotes
return fields
for line in fileinput.input():
listLine = getFields(line[:-1])
print listLine
This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:18:49 GMT