In [1]:
import csv
In [2]:
mesa = [
{'first': 'joe', 'last': 'lutz', 'address': 'aqui'},
{'first': 'mike', 'address': 'panera', 'last': 'duncan'},
{'address': 'nashville', 'first': 'travis', 'last': 'cash'},
]
In [3]:
mesa
Out[3]:
In [4]:
columns = ['last', 'first', 'address']
In [5]:
# excel_tab dialect was likely supported on creaky old version
# but required a hyphen instead of underscore.
with open('eggs.csv', 'wb') as csvfile:
writer = csv.DictWriter(csvfile, columns, dialect='excel-tab')
for row in mesa:
writer.writerow(row)
So do not write in binary mode.
That is a difference between the csv module of legacy Python and the csv module of modern Python.
In [6]:
with open('eggs.csv', 'w') as csvfile:
writer = csv.DictWriter(csvfile, columns, dialect='excel-tab')
for row in mesa:
writer.writerow(row)
In [7]:
!ls -l eggs.csv
In [8]:
!cat eggs.csv
In [9]:
print(repr(open('eggs.csv').read()))
Would like to have something like following to output the results of a query. Catherine probably knows the canonical example for such for an Oracle database.
In [ ]:
with open('eggs.csv', 'w') as csvfile:
writer = csv.DictWriter(csvfile, columns, dialect='excel-tab')
query_results = do_query(FOO_QUERY_SQL) # This is fakey stub.
for row in query_results:
writer.writerow(row)
In [ ]:
with open('eggs.csv', 'w') as csvfile:
writer = csv.DictWriter(csvfile, columns, dialect='excel-tab')
for row in do_query(FOO_QUERY_SQL): # This is a fakey stub.
writer.writerow(row)
In [ ]:
import psycopg2