2013-11-20 50 views
1

我正在使用psycopg2,我想知道是否有辦法從我的SELECT語句生成一些清潔輸出。Python postgres輸出格式化

我的數據是這樣的數據庫:

("Apple" user:(Apple) tag:(Apple)) 

當我拉下來的數據通過psycopg2,它看起來像這樣:

('("Apple" user:(Apple) tag:(Apple))') 

我想它不是那些附加字符到每行的前端和末尾,是否有更乾淨的方法來做到這一點?代碼如下。

cur.execute(''' 
    select field from table; 
    ''') 

rows = cur.fetchall() 

for row in rows: 
    # Write rows to text file 
    writer.write(row + '\n') 
+0

或writer.write(行[0] [1:-1] + '\ N') – uralbash

回答

0

你寫之前到文件,例如,你可以處理該行:

for row in rows: 
    # Write rows to text file 
    user = row[0] 
    tag = row[1] 
    writer.write(user + ' ' + tag + '\n') 
+0

究竟我在找什麼,謝謝! – solr