【发布时间】:2015-04-28 11:15:21
【问题描述】:
我正在尝试使用从 MongoDB 获取的数据和 Mongopy 将一些数据写入 CSV 格式。当前,当前设置了标头,问题是实际数据没有插入到 CSV 文件中。这是代码 sn-p :
from pymongo import MongoClient
import csv
import os
conn = pymongo.MongoClient()
db = conn.clixster_dev
cursor = db.channels.find({},{'_id':0 , 'company-reg-no':0, 'isdel':0 , 'last_off':0 , 'last_on':0 , 'online':0 , 'password':0 , 'psotcode':0 , 'state':0 , 'subagent':0})
outfile = open( "asdxk.csv", "w" )
# get a csv writer
writer = csv.writer( outfile )
# write header
writer.writerow(['postcode', 'upline', 'cit_state', 'contact_person', 'contact_no','nominal_level', 'credit', 'level', 'account_holder', 'merchantid', 'email', 'bank', 'reg_date' ,'address','acc_no','company_name'])
# write data
[ writer.writerow(x) for x in cursor ]
# close file
outfile.close()
不胜感激,
【问题讨论】:
-
为什么这不是写入文件。在文件操作中添加 try catch 块。检查写入数据时是否发生任何异常。只需调试代码
标签: python mongodb csv pymongo export-to-csv