import csv
DURATION = 5
FINISHED = 6
PID = 8
ATTENTION_1 = [40,41]
SEEN_HOME = 20
SEEN_FRIEND = 21
SEEN_NEIGHBOR = 22
SEEN_RENTAL = 23
SEEN_WORK = 24
OTHER_SITUATIONS = 25
DEVICES_OWNED = 26
HOME_CAMERA_PREF = [27, 31]
HOME_VOICE_PREF = [28, 32]
HOME_LIGHTS_PREF = [29, 33]
HOME_APPS_PREF = [30, 34]
ACTIONS_HOME = 35
WILLING_NEIGHBOR = [36, 37]
WILLING_HOUSEMATE = [38, 39]
WILLING_FRIEND = [42, 43]
WILLING_WORK = [44, 45]
NEIGHBOR_CAMERA_PREF = [46, 50, 54]
NEIGHBOR_VOICE_PREF = [47, 51, 55]
NEIGHBOR_LIGHTS_PREF = [48, 52, 56]
NEIGHBOR_APPS_PREF = [49, 53, 57]
ACTIONS_NEIGHBOR = 58
FRIEND_CAMERA_PREF = [59, 63]
FRIEND_VOICE_PREF = [60, 64]
FRIEND_LIGHTS_PREF = [61, 65]
FRIEND_APPS_PREF = [62, 66]
ACTIONS_FRIEND = 67
WORK_CAMERA_PREF = [68, 72, 76]
WORK_VOICE_PREF = [69, 73, 77]
WORK_LIGHTS_PREF = [70, 74, 78]
WORK_APPS_PREF = [71, 75, 79]
ACTIONS_WORK = 80
RENTAL_CAMERA_PREF = [81, 85, 89]
RENTAL_VOICE_PREF = [82, 86, 90]
RENTAL_LIGHTS_PREF = [83, 87, 91]
RENTAL_APPS_PREF = [84, 88, 92]
ACTIONS_RENTAL = 93
OTHER_SOLNS = 130
SOLNS_VERY_IMPORTANT = 94
SOLNS_SOMEWHAT_IMPORTANT = 95
SOLNS_NOT_IMPORTANT = 96
all_data = []
# Real data starts at row 3 -- previous rows are various types of headers
with open('feb5_data.csv', 'r') as original:
reader = csv.reader(original)
for row in reader:
all_data.append(row)
pref_lookup = {"Strongly appreciate": 7, "Appreciate": 6, "Somewhat appreciate": 5, "Neutral": 4,
"Somewhat dislike": 3, "Dislike": 2, "Strongly dislike": 1}
def process_prefs(data_row, which_prefs):
# input: a full row of data, and the variable that indexes some set of prefs (e.g., camera/work)
# output: an int from 1-7 corresponding to what this person's pref was
for i in which_prefs:
if data_row[i].strip() is not '':
pref = data_row[i].split(' - ')[0]
return(pref_lookup[pref])
def expand_seen(data_row, which_seen):
# input: a full row of data, and the variable that indexes some set of devices seen/not seen
# output: a list of booleans corresponding to whether each type of device was seen or not,
# and whether (if not, why not) the situation does not apply to the person
# list order is, [bool_camera, bool_voice, bool_lights, bool_apps, string_if_none]
ret_list = [False, False, False, False, 'n/a']
seen_str = data_row[which_seen]
if 'Camera' in seen_str:
ret_list[0] = True
if 'Voice' in seen_str:
ret_list[1] = True
if 'Light' in seen_str:
ret_list[2] = True
if 'Appliances' in seen_str:
ret_list[3] = True
if ret_list[:4] == [False, False, False, False]:
ret_list[4] = seen_str
return ret_list
def process_attention_1(data_row):
if data_row[ATTENTION_1[0]] == "It depends":
if data_row[ATTENTION_1[1]].lower() == "landlord":
return True
return False
# DEMO OF FUNCTIONS
print(process_prefs(all_data[3], WORK_APPS_PREF))
print(expand_seen(all_data[10], SEEN_RENTAL))
print(process_attention_1(all_data[15]))
4 [False, False, False, False, 'None of these'] True
def create_output_row(input_row):
# input: row of data from before
# output: row of data in new format (i.e., using functions above to process)
File "<ipython-input-58-c0e13ce55f09>", line 2 # input: row of data from before ^ IndentationError: expected an indented block
with open('feb5_data_processed.csv', 'w') as output:
# TODO: write header row (can be manual)
for old_row in all_data[4:]: # the 4 here is just because first few rows of original are headers; could remove those eralier
new_row = create_output_row(old_row)
# TODO: write new_row to file