split problem if the delimiter is inside the text limiter

Peter Otten __peter__ at web.de
Wed Mar 18 09:10:07 EDT 2009


rewonka wrote:

> I had a problem, i would like to process a file into a PSQL, but in
> the file the delimiter char is ':'
> and i have the same charater inside the text field also.
> something like this:
> text = 1:23:34:"sample: text":" something"
> if I use text.split(':')
> it will be ['1', '23', '34', '"sample', 'text"', 'something']
> but it's not good for me, because the delimiter is in text limiter  "
> "
> 
> is it a solution, that is fast to process ? so i don't need to split
> by "hand made" function

Have a look at http://docs.python.org/library/csv.html.

with open(filename, "rb") as instream:
    rows = csv.reader(instream, delimiter=":")
    # decode, put into db

Peter



More information about the Python-list mailing list