简体   繁体   中英

using a dictionary to parse a csv file

I have a dictionary of words from a questionnaire:

mydict = {'CP4 MPI(SF) throbbing [mild]':'throbbing mild',
CP4 MPI(SF) thobbing [moderate]':'throbbing moderate',....}

my csv file has numerous columns - that correspond to the keys in mydict, rows represent study participants.

if a row (a study participant) responded yes to a question, there is a 1 in that particular field of the csv file

the values contained within mydict are abbreviations of the persons response that I would like to save in a new list

I would like to parse a csv file using this dictionary as follows:

(using csv.DictReader)

mcgill = []

mcgill_sf = {'CP4 MPI(SF) | Throbbing [Mild]':'throbbing mild', 'CP4 
             MPI(SF) | Throbbing [Moderate]':'throbbing moderate', 'CP4 
             MPI(SF) | Throbbing [Severe]':'throbbing severe', 'CP4 
             MPI(SF) | Shooting[Mild]':'shooting mild'}

x = mcgill_sf.keys()
if row[x] == '1':
    y = mcgill_sf[key]
    mcgill.append(y)
print(mcgill)

Traceback (most recent call last):
  File "triage_1200.py", line 401, in <module>
    if row[x] == '1':
TypeError: unhashable type: 'dict_keys'

I can't determine how to work with keys / values within the dict to achieve this objective... can anyone advise?

I would suggest looking at pandas . It's designed for dealing with datasets such as these.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM