简体   繁体   English

python csv DictReader类型

[英]python csv DictReader type

I'm starting to code in python and i now have the problem, that the csv.DictReader gets me the wrong data type. 我开始在python中编码,我现在遇到了问题,csv.DictReader让我得到了错误的数据类型。

The csv file looks like: csv文件看起来像:

Col1, Col2, Col3 Col1,Col2,Col3

1,2,3 1,2,3

90,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? 是否可以定义列的值?

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: 或者这里是一个类型化的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}]

If you quote the non-numeric values in the csv file and initialize the reader by 如果引用csv文件中的非数字值并初始化阅读器

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

then numeric values will be automatically converted to floats. 那么数值将自动转换为浮点数。

I haven't used DictReader before, but you could just do this to the value: 我以前没有使用过DictReader,但你可以这样做:

...
for row in pol:
    col1 = float(row["Col1"]) # or int()
    ...

And then use col1 through out, you probably could also edit the dictionary: 然后使用col1,你可能也可以编辑字典:

row["Col1"] = float(row["Col1"])

But it depends what you want to use the row for later. 但这取决于您以后想要使用该行的内容。

It looks like you want Col1 to be an array of numbers, so you'd need to convert row["Col1"] to a number whether or not you were comparing it to a number. 看起来你希望Col1是一个数字数组,所以你需要将row [“Col1”]转换为数字,无论你是否将它与数字进行比较。 So convert it! 所以转换吧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM