Last active
December 26, 2015 12:09
-
-
Save makmac213/7149153 to your computer and use it in GitHub Desktop.
csv to class obj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class O: | |
""" | |
csv to class | |
o = O(['col_1_name','col_2_name',...]) | |
o.parse_row(row) | |
""" | |
def __init__(self, *args, **kwargs): | |
if len(args) == 1: | |
self.args = args[0] | |
for attr in args[0]: | |
attr = attr.strip().lower().replace(' ', '_') | |
attr = re.sub(r'[^a-zA-Z0-9_]', '', attr) | |
setattr(self, attr, '') | |
def parse_row(self, row): | |
for key, val in zip(self.args, row): | |
key = key.strip().lower().replace(' ', '_') | |
key = re.sub(r'[^a-zA-Z0-9_]', '', key) | |
setattr(self, key, val) | |
""" | |
import csv | |
rows = [] | |
with open('/home/markallan/test.csv', 'rb') as csvfile: | |
csvreader = csv.reader(csvfile) | |
for row in csvreader: | |
rows.append(row) | |
objs = [] | |
for i in range(1, len(rows)): | |
tmp_o = O(rows[0]) | |
tmp_o.parse_row(rows[i]) | |
objs.append(tmp_o) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment