How do I speedup this loop?

David Fraser davidf at
Tue Jul 13 10:17:09 CEST 2004

Marco Aschwanden wrote:
> On Tue, 13 Jul 2004 16:48:36 +1000, Unknown <unknown at unknown.invalid> 
> wrote:
>> I'm getting some output by running a command using os.popen. I need to 
>> parse the output and transform it in some sense so that it's 'DB 
>> compatible', (i.e I need to store the output in a database (postgres) 
>> after escaping some characters).
> If you are using Python's DB API 2.0 than this escaping would be done by 
> the API:
>>>> import odbc,dbi
>>>> con = odbc.odbc("DB_ID/USERNAME/PASSWORD")
>>>> cur = con.cursor()
>>>> sql = "INSERT INTO output (line) VALUES (?)"
>>>> dirty_line = 'Some text with forbidden characters\n\r...'
>>>> cur.execute(sql, dirty_line)
> So, no need to parse (and afterwards unparse) the ouput - I don't think 
> that anyone can beat this speed up!

Except if you're aiming for database independence, as different database 
drivers support different means of escaping parameters...


More information about the Python-list mailing list