-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/fspmarshall/scrape-util into…
… dev
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
from src.core.data_utils import Row | ||
import csv | ||
|
||
|
||
REQUIRE = [] | ||
|
||
|
||
# primary entry point of this parser. | ||
def parse(project,config,state,filepath): | ||
rawdata = read_csv(filepath) | ||
rows = reformat_data(config,rawdata) | ||
return state,rows | ||
|
||
# reformat the data into `Row` objects. | ||
def reformat_data(config,raw): | ||
if not raw: return | ||
for r in raw: print(len(r)) | ||
fields = ['url','survey','question','option','timestamp'] | ||
if raw[0] == fields: | ||
raw = raw[1:] | ||
if not raw: return | ||
toint = lambda r: [elem for elem in map(int,r)] | ||
torow = lambda r: Row(r[0],r[1],r[2],r[4],r[3]) | ||
mkrow = lambda r: torow(toint(r)) | ||
rows = [mkrow(r) for r in raw] | ||
return rows | ||
|
||
# because who doesn't like reading CSVs? | ||
def read_csv(filename): | ||
with open(filename) as fp: | ||
reader = csv.reader(fp) | ||
rows = [r for r in reader if r] | ||
return rows |