1

When I'm given a CSV file containing:

file1 = '"Name","Weight","Height","EyeColor"

"Jimmy","145","160","Blue"

"Kim","120","150","Brown"

"Sean","170","188","Red"'

My answer should return:

{'Jimmy': [(145,160,'Blue')], 'Kim': [(120,150,'Brown')], 'Sean': [(170,188,'Red')]}

enter image description here

There's my current code, i'd truly appreciate the help.

ChrisB
  • 2,497
  • 2
  • 24
  • 43

3 Answers3

1

If you don't want to use any modules, you could just split the file based on it's delimiter - in this case, ,. In so doing, you'd be left with a list of the data, with each individual attribute being an element in the list.

To get the dict the way you want it, we then use a list slice to divvy up the elements:

csv_dict = {}
for line in open('your_csv_file.csv', 'r').readlines()[1:]:
    line = line.replace('\n', '')
    line = line.split(',')
    csv_dict[line[0]] = line[1:] 

This will leave you with a dict with the names as keys and the other attributes as those keys values in a list.

n1c9
  • 2,662
  • 3
  • 32
  • 52
  • The code `csv_dict[line[0]]: line[1:]` does not works – Jay Rajput Nov 09 '16 at 18:54
  • The output is like this `{'"Name"': ['"Weight"', '"Height"', '"EyeColor"\n'], '"Kim"': ['"120"', '"150"', '"Brown"\n'], '"Jimmy"': ['"145"', '"160"', '"Blue"\n'], '"Sean"': ['"170"', '"188"', '"Red"\'\n']}' which is slight different from the answer...but I got the idea.. – Jay Rajput Nov 09 '16 at 18:56
  • Edited version should fix that up. – n1c9 Nov 09 '16 at 18:58
  • To get integer weight and height `csv_dict[line[0]] = (int(line[1], int(line[2], line[3])` – Jay Rajput Nov 09 '16 at 19:04
0

I changed your function a little bit. The code follows bellow:

def readf(somefile):
    my_dict = {}
    with open(somefile, 'r') as f:
        for line in f.readlines()[1:]:
            line = line.rstrip()
            line = line.replace('"', '')
            items = line.split(',')
            my_dict[items[0]] = [int(items[1]), int(items[2]), items[3]]
    return my_dict

# print keys in alphabetical order
my_list = list(my_dict.keys())
my_list.sort()
print(my_list)
daniboy000
  • 1,069
  • 2
  • 16
  • 26
  • Thanks, say the names werent already in alphabetical order. How would be able arrange them by values because I know you can't sort keys but you can values? – Andrew Donnelley Nov 09 '16 at 19:50
  • You can put the keys in a list and sort it. AFAIK you can't sort the keys in a dictionary. Se http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key for more info. – daniboy000 Nov 09 '16 at 19:57
  • You can save your data in a pandas Dataframe, that way, you can define the names as keys and sort them. – daniboy000 Nov 09 '16 at 20:04
  • What if there was another guy named Jimmy and he had different characteristics but i wanted to put both tuples under the name Jimmy. Does the code already take care of that because of the my_dict[item[0]]=[item1...]? – Andrew Donnelley Nov 09 '16 at 20:48
  • Your code would replace the old Jimmy for the new Jimmy. One thing that you could do is to check if items[0] already exists as a key in my_dict using if my_dict.has_key(items[0]): If the answer is true you could append the new value with the old value, creating a list with two persons. – daniboy000 Nov 09 '16 at 23:43
0
In [21]: def readf(somefile):
...:     my_dict = {}
...:     with open(somefile, 'r') as f:
...:         for i, line in enumerate(f):
...:             if i == 0:
...:                 continue
...:             line = line.replace('\"', '')
...:             line = line.replace('\n', '')
...:             items = line.split(',')
...:             key, values = items[0], [(int(items[1]), int(items[2])], items[3])
...:             my_dict[key] = values
...:     return my_dict
...:

In [22]: d = readf('file1')

In [23]: d
Out[23]:
{'Jimmy': [(145, 160, 'Blue')],
 'Kim': [(120, 150, 'Brown')],
 'Sean': [(170, 188, 'Red')]}
Swagga Ting
  • 602
  • 3
  • 17