[英]Including column names when converting an .mdb file to .csv in Python
I've managed to convert a .mdb file to .csv, based on this post: How to export MS Access table into a csv file in Python using eg pypyodbc .根据这篇文章,我设法将 .mdb 文件转换为 .csv: How to export MS Access table into a csv file in Python using eg pypyodbc 。
However, I can not get the metadata (column name) from the original file.但是,我无法从原始文件中获取元数据(列名)。 Does anyone have a clue on how to do that?
有没有人知道如何做到这一点?
Thanks!谢谢!
Simply retrieve headers from cursor.description
where you will call writerow
just before iterating through cursor results:只需从
cursor.description
检索标题,您将在迭代游标结果之前调用writerow
:
# OPEN CSV AND ITERATE THROUGH RESULTS
with open('CSVDatabaseWithHeaders.csv', 'w', newline='') as f:
writer = csv.writer(f)
# ADD LINE BEFORE LOOP
writer.writerow([i[0] for i in cur.description])
for row in cur.fetchall() :
writer.writerow(row)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.