Stephan Richter wrote:
On Thu, 04 Nov 1999, Max M wrote:
I am trying to import a comma delimited ascii file via Python.
naturally the pattern: , will not work as there might be a comma inside the text.
the pattern "," wont work either as there as the numbers are not enclosed in quotes.
There must be a simple pattern doing it right but I cannot seem to figure it out myself.
Look, then your datafile is corrupt. If you have commas in the text fields then commas as seperator does not work. Period.
Not if all fields were delimited by double quotes ("). The algorithm would still be fairly simple then. But now numbers *aren't* enclosed by quotes, which makes it rather more complicated to write, but it's still possible. This would get even more complicated if you do have escaped \" in strings. :)
Choose another delimitor such as TAB. I wrote a long while back a parser for these things. It is trivial. Just a few lines of code.
If you can choose your own datafile, that's best way to do it. If you already have a datafile that you can't control, you need to convert it to a tab delimited file first, for which you'd use Python, and you're stuck with the same problem. ;) Anyway, if you need more help with Python I'd suggest looking in comp.lang.python. Possibly there are modules you can download that do this, too, at www.python.org. Regards, Martijn