CSV(???)
David C. Ullrich
ullrich at math.okstate.edu
Sat Feb 24 06:30:00 EST 2007
On 23 Feb 2007 19:13:10 +0100, Neil Cerutti <horpner at yahoo.com> wrote:
>On 2007-02-23, David C Ullrich <ullrich at math.okstate.edu> wrote:
>> Is there a csvlib out there somewhere?
>>
>> And/or does anyone see any problems with
>> the code below?
>>
>> [...]
>>
>> (Um: Believe it or not I'm _still_ using python 1.5.7. So
>> comments about iterators, list comprehensions, string methods,
>> etc are irrelevent. Comments about errors in the algorithm
>> would be great. Thanks.)
>
>Two member functions of indexedstring are not used: next and
>lookahead. __len__ and __getitem__ appear to serve no real
>purpose.
Hey, thanks! I didn't realize that using an object with
methods that were never called could cause an algorithm
to fail... shows how much I know.
(Seriously, all I really wanted to know was whether anyone
noticed something I overlooked, so that
parsecsvline(csvline(fields)) might under some condition
not come out the same as fields...)
>> def parsecsvline(csvline):
>> """Inverts csvline(). Assumes csvline is valid, ie
>> is something as returned by csvline(); output undefined
>> if csvline is in invalid format"""
>>
>> s = indexedstring(csvline)
>> res = []
>>
>> while not s.eos():
>> res.append(s.getfield())
>>
>> return res
>
>You'll be happy to know that iterators and list comprehensions
>will make your code better after you upgrade. ;-)
Uh, thanks again. You're right, knowing that makes me so happy
I could just burst.
>In the meantime, I think your (relative lack of) error handling
>is OK. GIGO, as they say (garbage in, garbage out).
_I_ don't think it's ok.
But (i) the code I posted was not supposed to be the final
version! It was a preliminary version, posted hoping that
someone would notice any errors in the _algorithm_ that
existed. (ii) in the intended application parsecsvline
will only be applied to the output of csvline, so if
the former is indeed a left inverse of the latter there
should be no error unless something else has already
gone wrong elsewhere. Not that that makes it ok...
************************
David C. Ullrich
More information about the Python-list
mailing list