-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposts.py
68 lines (62 loc) · 1.91 KB
/
posts.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
import MySQLdb
from xml.dom import minidom
xmldoc=minidom.parse("Posts.xml")
rowele=xmldoc.getElementsByTagName('row')
db = MySQLdb.connect("localhost","root","PASSWORD","DATABASE")
cursor = db.cursor()
for i in range(len(rowele)):
try :
postid = str(rowele[i].attributes['PostTypeId'].value)
except :
postid = 'NULL'
try :
accansid = str(rowele[i].attributes['AcceptedAnswerId'].value)
except :
accansid = 'NULL'
try :
score = str(rowele[i].attributes['Score'].value)
except :
score = 'NULL'
try :
viewcount = str(rowele[i].attributes['ViewCount'].value)
except :
viewcount = 'NULL'
try :
body = rowele[i].attributes['Body'].value
body =str(filter(lambda x:ord(x)>31 and ord(x)<128,body))
body = MySQLdb.escape_string(body)
except :
body = 'NULL'
try :
title = rowele[i].attributes['Title'].value
title = str(filter(lambda x:ord(x)>31 and ord(x)<128,title))
title = MySQLdb.escape_string(title)
except :
title = 'NULL'
try :
tags = rowele[i].attributes['Tags'].value
tags = str(filter(lambda x:ord(x)>31 and ord(x)<128,tags))
tags = MySQLdb.escape_string(tags)
except :
tags = 'NULL'
try :
date = str(rowele[i].attributes['CreationDate'].value)
date = MySQLdb.escape_string(date)
except :
date = 'NULL'
try :
anscount = str(rowele[i].attributes['AnswerCount'].value)
except :
anscount = 'NULL'
try :
favcount = str(rowele[i].attributes['FavoriteCount'].value)
except :
favcount = 'NULL'
try :
comcount = str(rowele[i].attributes['CommentCount'].value)
except :
comcount = 'NULL'
insert_sql = "INSERT INTO `Posts` (`PostTypeId`,`AcceptedAnswerId`,`Score`,`Body`,`CreationDate`,`ViewCount`,`Title`,`Tags`,`AnswerCount`,`FavoriteCount`,`CommentCount`) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" %(postid,accansid,score,body,date,viewcount,title,tags,anscount,favcount,comcount)
print insert_sql
cursor.execute(insert_sql)
db.commit()