通过python写函数重新格式化mongodb
我试图通过Python将MongoDB中的文档拉入CSV格式的文本文件中。问题是来自Mongo的内容在新文件的每条记录中都包含头信息。例如:通过python写函数重新格式化mongodb,python,mongodb,Python,Mongodb,我试图通过Python将MongoDB中的文档拉入CSV格式的文本文件中。问题是来自Mongo的内容在新文件的每条记录中都包含头信息。例如: u'mac': u'001dd07c7a90', u'opt80': u'dslforum.org', u'ip': u'10.242.9.30', u'hostname': u'pnrg',and etc 我希望以表格格式写入文件(即 您可能想看看内置的csv Python模块 您查看过python的吗? mac opt80
u'mac': u'001dd07c7a90', u'opt80': u'dslforum.org', u'ip': u'10.242.9.30', u'hostname': u'pnrg',and etc
我希望以表格格式写入文件(即
您可能想看看内置的csv Python模块
您查看过python的吗? mac opt80 ip hostname 001dd07c7a90 dslforum.org 10.242.9.30 pnrg
# Open a file csv format to send reformated Mongo data to it
fo = open('Merged_Data_Output.txt', 'w')
# Finding all hostname records containing 'pnrg'
opnrg = db.systemlog1.find({'hostname': 'pnrg'}).limit(5)
# Writing documents into a table format
for rcds in pnrg:
print rcds
fo.write(str(rcds))
fo.write('\n')
fo.close()