-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_parser.py
More file actions
44 lines (37 loc) · 1.3 KB
/
example_parser.py
File metadata and controls
44 lines (37 loc) · 1.3 KB
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
from globalgiving_utilities import Organization
import re
def parse_record(entry):
fields = entry.split(',')
if fields[0] == "" or fields[0] == "id":
return
new_organization = Organization()
new_organization.id = "o.example.{}".format(fields[0])
new_organization.source = "example"
new_organization.name = ' '.join([i.capitalize() for i in fields[1].split(' ')])
new_organization.add_registration("US", fields[0])
new_organization.ids["example"] = fields[0]
if fields[2]:
website = fields[2]
if not bool(re.match('http', website, re.I)):
website = "http://" + website
new_organization.website = website
address = {
"address_1": fields[3] or None,
"city": fields[4] or None,
"state": fields[5] or None,
"postal": fields[6] or None,
"country": "United States"
}
new_organization.mailing_address = {key: value for key, value in address.items() if value}
print(new_organization.to_dict())
# Upload the data
new_organization.upload()
def parse_file(filename):
with open(filename) as data_file:
for entry in data_file:
entry = entry.rstrip()
parse_record(entry)
def main():
parse_file("example_data.csv")
if __name__ == '__main__':
main()