Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv DictReader type

Tags:

python

types

csv

I'm starting to code in python and i now have the problem, that the csv.DictReader gets me the wrong data type.

The csv file looks like:

Col1, Col2, Col3

1,2,3

90,2,3

pol = csv.DictReader(open('..\data\data.csv'),dialect='excel')

Col1 = []

for row in pol:
    if row["Col1"] < 90:
        Col1.append(row["Col1"] * 1.5)
    else:
        Col1.append("Col1")

I get the following error:

if row["Col1"] < 90:
TypeError: unorderable types: str() < int()

I won't convert every single value. Is it possible to define the values of the column?

like image 563
user1132891 Avatar asked Jan 05 '12 19:01

user1132891


People also ask

What is CSV DictReader Python?

CSV, or "comma-separated values", is a common file format for data. The csv module helps you to elegantly process data stored within a CSV file. Also see the csv documentation.

What is the difference between CSV reader and CSV DictReader?

csv. Reader() allows you to access CSV data using indexes and is ideal for simple CSV files. csv. DictReader() on the other hand is friendlier and easy to use, especially when working with large CSV files.

What does CSV DictReader return?

The csv. DictReader() returned an OrderedDict type for each row. That's why we used dict() to convert each row to a dictionary.

What is a DictReader in Python?

Python CSV DictReader DictReader class operates like a regular reader but maps the information read into a dictionary. The keys for the dictionary can be passed in with the fieldnames parameter or inferred from the first row of the CSV file.


1 Answers

You could use a library like pandas, it will infer the types for you (it's a bit of an overkill but it does the job).

import pandas
data = pandas.read_csv(r'..\data\data.csv')
# if you just want to retrieve the first column as a list of int do
list(data.Col1)
>>> [1, 90]

# to convert the whole CSV file to a list of dict use
data.transpose().to_dict().values()
>>> [{' Col2': 2, ' Col3': 3, 'Col1': 1}, {' Col2': 2, ' Col3': 3, 'Col1': 90}]

Alternatively here is an implementation of a typed DictReader:

from csv import DictReader
from itertools import imap, izip

class TypedDictReader(DictReader):
  def __init__(self, f, fieldnames=None, restkey=None, restval=None, \
      dialect="excel", fieldtypes=None, *args, **kwds):

    DictReader.__init__(self, f, fieldnames, restkey, restval, dialect, *args, **kwds)
    self._fieldtypes = fieldtypes

  def next(self):
    d = DictReader.next(self)
    if len(self._fieldtypes) >= len(d) :
      # extract the values in the same order as the csv header
      ivalues = imap(d.get, self._fieldnames) 
      # apply type conversions
      iconverted = (x(y) for (x,y) in izip(self._fieldtypes, ivalues)) 
      # pass the field names and the converted values to the dict constructor
      d = dict(izip(self._fieldnames, iconverted)) 

    return d

and here is how to use it:

reader = TypedDictReader(open('..\data\data.csv'), dialect='excel', \
  fieldtypes=[int, int, int])
list(reader)
>>> [{' Col2': 2, ' Col3': 3, 'Col1': 1}, {' Col2': 2, ' Col3': 3, 'Col1': 90}]
like image 142
Max Avatar answered Nov 07 '22 03:11

Max