-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_population_files.py
95 lines (78 loc) · 2.81 KB
/
parse_population_files.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
import csv
def parseUsers(filename):
if type(filename) != str:
raise ValueError("Input must be a string")
users = {}
try:
with open(filename, mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
user = row.pop("username")
users[user] = row
line_count += 1
print(f' {line_count} lines.')
except:
raise TypeError("%s must be a csv file" % filename)
return users
def parsePosts(filename):
if type(filename) != str:
raise ValueError("Input must be a string")
posts = {}
try:
with open(filename, mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
user = row.pop("user")
posts[user] = posts.get(user, [])
posts[user].append(row)
line_count += 1
print(f' {line_count} lines.')
except:
raise TypeError("%s must be a csv file" % filename)
return posts
def parseCatrgories(filename):
"""returns a list of tuples where each tuple stores the name and filename of the category"""
if type(filename) != str:
raise ValueError("Input must be a string")
categories = []
try:
with open(filename, mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
categories.append(tuple(row.values()))
line_count += 1
print(f' {line_count} lines.')
except:
raise TypeError("%s must be a csv file" % filename)
return categories
def parseComments(filename):
"""returns a list of comments"""
if type(filename) != str:
raise ValueError("Input must be a string")
comments = []
try:
with open(filename, mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
comments.append(tuple(row.values())[0])
line_count += 1
print(f' {line_count} lines.')
except:
raise TypeError("%s must be a csv file" % filename)
return comments