split problem if the delimiter is inside the text limiter

rewonka rewonka at gmail.com
Wed Mar 18 15:17:47 EDT 2009


On márc. 18, 14:10, Peter Otten <__pete... at web.de> wrote:
> 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 athttp://docs.python.org/library/csv.html.
>
> with open(filename, "rb") as instream:
>     rows = csv.reader(instream, delimiter=":")
>     # decode, put into db
>
> Peter

Thanks foe the answers.

I tried that module it's work fine.
Now i stucked when i tried to pu into db.
Because i have some cell that is in somekind of unicoded text, and i'm
looking a solution how to put this into db (my db in utf-8 format).
Im using the pgdb modul, something like this:

# connection is an object from pgdb class

sql = ''' INSERT INTO table (column1,column2, ...) VALUES ( %s,
%s, ....); '''
rows = csv.reader(open("sample.csv","rb"), delimiter=":",
quotechar='"')
for row in rows:
    connection.cursor.execute(sql % (row[0],row[1],....))
connection.corsur.commit()

but something binary in a cell, the pgdb says it is not in utf-8
format, or something like this.
I know it's a newbie solution :))
better solution?

Rew



More information about the Python-list mailing list