Python - make a dictionary from a csv file with multiple categories -


i trying make dictionary csv file in python, have multiple categories. want keys id numbers, , values name of items. here text file:

"id#","name","quantity","price" "1","hello kitty","4","9999" "2","rilakkuma","3","999" "3","keroppi","5","1000" "4","korilakkuma","6","699" 

and have far:

txt = open("hk.txt","ru")  file_data = txt.read() lst = []           #first make list, , convert dictionary. key in file_data:         k = key.split(",")         lst.append((k[0],k[1]))  dic = dict(lst) print(dic) 

this prints empty list though. want keys id#, , values names of products. make dictionary names keys , id#'s values, think same thing other way around.

you can use dictionary directly:

dictionary = {} file_data.readline()   # skip first line          key in file_data:     key = key.replace('"', '').strip()     k = key.split(",")     dictionary[k[0]] = k[1] 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -