Python - CSV to Matrix
Question: Can you help me with this problem?

I`m new in programming and want to find out how to create a matrix, which
looks like this:

    
    
    matrix = {"hello":["one","two","three"],
              "world": ["five","six","seven"],
              "goodbye":["one","two","three"]}
    

I want to import a csv, which has all the strings (one, two three,...) in it
and I tried with the split method, but I`m not getting there... Another
problems are the names of the categories (hello, world, goodbye)

Do you have any suggestions?


Answer: have you looked into the csv module?
<https://docs.python.org/2/library/csv.html>

    
    
    import csv
    
    TEST_TEXT = """\
    hello,one,two,three
    world,four,five,six
    goodbye,one,two,three"""
    
    TEST_FILE = TEST_TEXT.split("\n")
    #file objects iterate over newlines anyway
    #so this is how it would be when opening a file
    
    #this would be the minimum needed to use the csv reader object:
    for row in csv.reader(TEST_FILE):
        print(row)
    
    #or to get a list of all the rows you can use this:
    as_list = list(csv.reader(TEST_FILE))
    
    #splitting off the first element and using it as the key in a dictionary
    dict_I_call_matrix = {row[0]:row[1:] for row in csv.reader(TEST_FILE)}
    print(dict_I_call_matrix)
    
    
    
    without_csv = [row.split(",")  for row in TEST_FILE] #...row in TEST_TEXT.split("\n")]
    
    matrix = [row[1:] for row in without_csv]
    labels = [row[0] for row in without_csv]
    

