-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive.parser.py
79 lines (55 loc) · 2.06 KB
/
archive.parser.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
##############################################################################
# Daniel Farnand #
# 12 April 2017 #
# Code to import the Stackexchange data from archive.org #
##############################################################################
# Observations:
# 1. Posts.xml
# - Each row is a post
import xml.etree.ElementTree as etree
posts = etree.parse("ai.stackexchange/Posts.xml").getroot()
attribs = list()
for post in posts:
attribs.append(post.attrib)
###########################################
# Examples of how to pull data from this: #
###########################################
# The list of all attributes we can pick from
attribs[1].keys()
# Manually pulling specific values.
attribs[1].get('Id')
# Getting a list of all post text
postText = list()
for post in attribs:
postText.append(post.get('Body'))
# This just a list of strings - note that this contains html formatting. It should be useful, especially for separating code out from prose.
print(postText[13:16])
# Getting list of scores
Scores = list()
for post in attribs:
Scores.append(post.get('Score'))
#################################################
# Seeing about matching with Comments and Votes #
#################################################
comments = etree.parse("ai.stackexchange/Comments.xml").getroot()
votes = etree.parse("ai.stackexchange/Votes.xml").getroot()
commAtt = list()
for c in comments:
commAtt.append(c.attrib)
voteAtt = list()
for v in votes:
voteAtt.append(v.attrib)
i = 1
attribs[i]
commAtt[i]
voteAtt[i]
# Observations
# - We can use 'PostID' to connect sets from different data.
# - I believe this corresponds with 'Id' in Posts
# Looking at the above comment, we have a comment to postid 7. In order to
# retrieve this post:
for p in attribs:
if p.get('Id') == '7':
print(p)
# Goal - more efficient way to do this. Probably involves putting the info all
# together into a data frame.