簡體   English   中英

在寫入CSV之前將列附加到python光標?

[英]Append columns to python cursor before writing to CSV?

#!/usr/bin/env python
import csv
import pyodbc

out_file = "file.csv"

conn = pyodbc.connect("DRIVER={driver};SERVER=servername;UID=user;PWD=pass;DATABASE=data")

cursor = conn.execute("SELECT TOP 10 * FROM table")

with open(out_file, 'w') as f:
  print cursor
  csv.writer(f, quoting=csv.QUOTE_MINIMAL).writerows(cursor)

這是我現在的代碼,並且按預期工作。 我的問題是,在將其插入CSV之前,如何在行中添加一些空值列? 我需要這樣做,因為我將它與另一個將要有一些額外數據列的查詢相結合,我希望數據與空填充列對齊。

怎么樣:

writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
for row in cursor:
    writer.writerow(list(row) + ['', '', '', ''])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM