-
Notifications
You must be signed in to change notification settings - Fork 47
/
to_csv.py
58 lines (51 loc) · 1.37 KB
/
to_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import csv
from pymongo import MongoClient
import time
client=MongoClient()
db=client['my_zhihu_data']
con=db['zhihu__user__profile']
def mongo_to_csv():
CNT=0
with open('zhihu_user_data_30k.csv', "wb") as f:
start_time = time.time()
csv_writer = csv.writer(f)
csv_writer.writerow([
'用户名',
'被赞同数',
'被感谢数',
'关注了',
'关注者',
'学校',
'专业',
'公司',
'职位',
'所在地',
'性别',
'相关说明',
'个性签名',
'url'])
for user in con.find():
csv_writer.writerow([
user['user_name'],
user['user_be_agreed'],
user['user_be_thanked'],
user['user_followees'],
user['user_followers'],
user['user_education_school'],
user['user_education_subject'],
user['user_employment'],
user['user_employment_extra'],
user['user_location'],
user['user_gender'],
user['user_info'],
user['user_intro'],
user['user_url']
])
CNT+=1
print "to csv,it cost:", time.time() - start_time
print "Total %d" %CNT