[Csv] CSV module. DictReader uses string values instead of int

Guido Davidzon gdavidzon at gmail.com
Wed Jul 29 23:11:01 CEST 2009


Hi,

I am new to Python and I am trying to use the CSV module to import a  
dataset as a dictionary data structure. The reason I am doing this, is  
to find similarities between instances of my dataset using similarity  
algorithms (like pearson). These algorithms take data in dictionary  
format and then compute similarities between any given instances.
The problem I encountered is that when importing a CSV file using the  
CSV Module, the values of each key are represented in a string format  
instead of integer. Hence, when running the algorithm using the  
imported dataset in the dictionary, I get this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "readcsv.py", line 99, in sim_pearson
for item in prefs[p1]:
TypeError: string indices must be integers, not str

My question is: Is there a way of importing the values of the  
dictionary in integer instead of string? I know that variables are  
immutable in python, but then what can I do?

Using DictReader from the CSV module, my dictionary looks like this:

mydict={'variable1': '0', 'variable2': '1', 'variable3': '0',  
'variable4': '1'}

and I want it to look like this:

mydict={'variable1': 0, 'variable2': 1, 'variable3': 0, 'variable4': 1}

Thanks in advance,

- Guido


More information about the Csv mailing list