excel 保存方法:
book = xlwt.Workbook()
#创建表单
sheet = book.add_sheet(u'sheet1',cell_overwrite_ok=True)
sheet.write(0,0,'id')
sheet.write(0,1,'text')
sheet.write(0,2,'user_id')
sheet.write(0,3,'geo_coordinates1')
sheet.write(0,4,'geo_coordinates2')
sheet.write(0,5,'created_at')
i =1
for all in searchRes:
sheet.write(i,0,all['_id'])
sheet.write(i,1,all['text'])
sheet.write(i,2,all['user_id'])
sheet.write(i,3,all['geo']['coordinates'][0])
sheet.write(i,4,all['geo']['coordinates'][1])
sheet.write(i,5,all['created_at'])
i=i+1
if(i==65530):
break
book.save('Excel_Workbook.xls')
workshop 的写入方法十分简洁:使用write函数
write(i,j,data) ##i,j分别为excel的i行j列的位置。起始为0,0
最后记得使用save
注意:excel由于限制最多存 65536行数据,因此面对海量的数据我们使用文本txt保存
result如下。
txt保存方法:
f = open('test1.txt','w',encoding='utf-8')
f.write('id')
f.write('\t')
f.write('text')
f.write('\t')
f.write('user_id')
f.write('\t')
f.write('geo_coordinates1')
f.write('\t')
f.write('geo_coordinates2')
f.write('\t')
f.write('created_at')
f.write('\n')
for all in searchRes:
f.write(str(all['_id']))
f.write('\t')
f.write(all['text'])
f.write('\t')
f.write(str(all['user_id']))
f.write('\t')
f.write(str(all['geo']['coordinates'][0]))
f.write('\t')
f.write(str(all['geo']['coordinates'][1]))
f.write('\t')
f.write(all['created_at'])
f.write('\n')
f.close()
比excel更为简洁。直接上代码如上。