forked from mdamien/stackoverflow-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_csv.py
158 lines (130 loc) · 5.12 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import json
import sys
import os
import xmltodict
import csv
from os.path import join
from utils import *
import shutil
PATH = sys.argv[1]
DIR = PATH.replace('extracted/', '')
print("importing", DIR)
file = join(PATH, 'Posts.xml')
def clean(x):
# neo4j-import doesn't support: multiline (coming soon), quotes next to each other and escape quotes with '\""'
return x.replace('\n', '').replace('\r', '').replace('\\', '').replace('"', '')
def open_csv(name):
return csv.writer(open('csvs/{}.csv'.format(name), 'w', encoding='utf-8'), doublequote=False, escapechar='\\')
try:
shutil.rmtree('csvs/')
except:
pass
os.mkdir('csvs')
posts = open_csv('posts')
posts_rel = open_csv('posts_rel')
users = open_csv('users')
users_posts_rel = open_csv('users_posts_rel')
votes = open_csv('votes')
votes_users_rel = open_csv('votes_users_rel')
votes_posts_rel = open_csv('votes_posts_rel')
comments = open_csv('comments')
comments_users_rel = open_csv('comments_users_rel')
comments_posts_rel = open_csv('comments_posts_rel')
posts_things = ['posttypeid', 'acceptedanswerid', 'creationdate', 'deletiondate', 'score', 'viewcount', 'body',
'ownerdisplayname', 'lasteditoruserid', 'lasteditordisplayname', 'lasteditdate', 'lastactivitydate',
'title', 'answercount', 'commentcount', 'favoritecount', 'closeddate', 'communityowneddate']
posts.writerow(['postId:ID(Post)'] + posts_things)
posts_rel.writerow([':START_ID(Post)', ':END_ID(Post)'])
users_things = ['reputation', 'creationdate', 'displayname', 'lastaccessdate', 'websiteurl', 'location', 'profileimageurl', 'aboutme',
'views', 'upvotes', 'downvotes', 'age', 'accountid', 'emailhash']
users.writerow(['userId:ID(User)'] + users_things)
users_posts_rel.writerow([':START_ID(User)', ':END_ID(Post)'])
votes_things = ['votetypeid', 'creationdate', 'bountyamount']
votes.writerow(['voteId:ID(Vote)'] + votes_things)
votes_users_rel.writerow([':START_ID(User)', ':END_ID(Vote)'])
votes_posts_rel.writerow([':START_ID(Post)', ':END_ID(Vote)'])
comments_things = ['score', 'text', 'creationdate', 'userdisplayname']
comments.writerow(['commentId:ID(Comment)'] + comments_things)
comments_users_rel.writerow([':START_ID(User)', ':END_ID(Comment)'])
comments_posts_rel.writerow([':START_ID(Post)', ':END_ID(Comment)'])
for i, line in enumerate(open(file, encoding='utf8')):
line = line.strip()
try:
if line.startswith("<row"):
el = xmltodict.parse(line)['row']
el = replace_keys(el)
row = [el['id'], ]
for k in posts_things:
row.append(clean(el.get(k, '')))
posts.writerow(row)
if el.get('parentid'):
posts_rel.writerow([el['parentid'], el['id']])
if el.get('owneruserid'):
users_posts_rel.writerow([el['owneruserid'], el['id']])
if el.get('tags'):
eltags = [x.replace('<', '') for x in el.get('tags').split('>')]
# for tag in [x for x in eltags if x]:
# tags_posts_rel.writerow([el['id'], tag])
except Exception as e:
print('x', e)
if i and i % 100000 == 0:
print('.', end='')
if i and i % 1000000 == 0:
print(i)
print(i, 'posts ok')
file = join(PATH, 'Users.xml')
for i, line in enumerate(open(file, encoding='utf-8')):
line = line.strip()
try:
if line.startswith("<row"):
el = xmltodict.parse(line)['row']
el = replace_keys(el)
row = [el['id'], ]
for k in users_things:
row.append(clean(el.get(k, '')))
users.writerow(row)
except Exception as e:
print('x', e)
if i % 100000 == 0:
print('.', end='')
print(i, 'users ok')
file = join(PATH, 'Votes.xml')
for i, line in enumerate(open(file, encoding='utf-8')):
line = line.strip()
try:
if line.startswith("<row"):
el = xmltodict.parse(line)['row']
el = replace_keys(el)
row = [el['id'], ]
for k in votes_things:
row.append(clean(el.get(k, '')))
votes.writerow(row)
if el.get('postid'):
votes_posts_rel.writerow([el['postid'], el['id']])
if el.get('userid'):
votes_users_rel.writerow([el['userid'], el['id']])
except Exception as e:
print('x', e)
if i % 100000 == 0:
print('.', end='')
print(i, 'votes ok')
file = join(PATH, 'Comments.xml')
for i, line in enumerate(open(file, encoding='utf-8')):
line = line.strip()
try:
if line.startswith("<row"):
el = xmltodict.parse(line)['row']
el = replace_keys(el)
row = [el['id'], ]
for k in comments_things:
row.append(clean(el.get(k, '')))
comments.writerow(row)
if el.get('postid'):
comments_posts_rel.writerow([el['postid'], el['id']])
if el.get('userid'):
comments_users_rel.writerow([el['userid'], el['id']])
except Exception as e:
print('x', e)
if i % 100000 == 0:
print('.', end='')
print(i, 'comments ok')