How to create variables from an CSV file in Python
Question: I am an absolut noobie in coding. So I have a problem to solve. First I have a
CSV file looking like this for example:

text.csv:

> jan1,A  
>  jan2,B  
>  jan3,C  
>  jan4,A  
>  jan5,B  
>  jan6,C

Now I want to import this "data" from the CSV in a Python programm, so that
the variables are made directly from the CSV file:

    
    
    jan1=A
    jan2=B
    ...
    

Please remark that the `A` should not be imported as a string, it`s a
variable. When I import the CSV with CSV reader all the data is imported as a
string?


Answer: It sounds like you want to "cross the boundary" between data and code by
turning data _into_ code. This is generally discouraged because it can be
dangerous (what if someone added a command to your csv file that wiped your
computer?).

Instead, just split the lines in the file by comma and save the first part,
the variable name, as a dictionary key and make the second part the value:

    
    
    csv_dict = {}
    
    with open(csv_file, "r") as f:
        for line in f:
            key_val = line.strip().split(",")
            csv_dict[key_val[0]] = key_val[1]
    

You can now access the keys/values via dictionary lookup:

    
    
    >>> csv_dict["jan5"]
    'B'
    >>> csv_dict["jan4"]
    'A'
    >>> my_variable = data_dict["jan4"]
    >>> my_variable
    'A'
    

