CSV阅读器和DictReader将数字字段转换为字符串

时间:2013-09-29 15:52:21

标签: python csv

csv的第一行有标题。 这是我的csv示例行:

2013-07-31 00:00:00,,1.0,2013.0,7.0,Q3,21160742,32HHBS1307170203,KL0602130731,AIRFRANCE 
KLM,KLM,KLM,KLM,KL,KLM ROYAL DUTCH AIRLINES,,0602,,KL0602,KL,KLM ROYAL DUTCH
 AIRLINES,,,,KL,0602,,,LAX,AMS,,31-7-2013 0:00:00,2013-07-31,2013-07-31,2013-07-31,2013-07-31,
13:55:00,14:39:00,20:55:00,21:39:00,2013-08-01,2013-08-01,2013-08-01,2013-08-01,
09:05:00,09:45:00,07:05:00,07:45:00,2.0,,2,,,LAX,LOS ANGELES INTERNATIONAL AIRPORT,
LAX,LAX,5.0,LAX,LOS ANGELES,US,UNITED STATES OF AMERICA,US,USA,NA8,NORTHERN AMERICA,
AMERICAS,,,,AMS,SCHIPHOL I,F,OFFLINE,I,INDIRECT OFFLINE,14.0,3.0,FRONT,Business,2.0,nan,
PLANNED,3.0,,2.0,2.0,34.0,4.0,400254887nan,1.0,2.0,2.0,2.0,1.0,2.0,6.0,3.0,1.0,3.0,1.0,1.0,
nan,nan,nan,nan,nan,nan,nan,3.0,3.0,3.0,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,
nan,2.0,2.0,2.0,2.0,2.0,7.0,nan,2.0,3.0,3.0,3.0,3.0,nan,nan,nan,nan,nan,nan,nan,nan,nan,
nan,nan,nan,nan,6.0,1.0,nan,nan,nan,nan,nan,2.0,nan,nan,nan,nan,nan,nan,nan,nan,nan,2.0,2.0,
nan,2.0,nan,3.0,nan,,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,13.7885862654653,
0.2, 34273499844164,nan,37.0,Booked,35.0,10.0,2.0,2.0,6.0,35.0,10.0,42.0,nan,nan,LAX,LAX,N

如果我使用input_file = csv.DictReader(open("file.csv")input_file = csv.reader(open('file.csv')),我的所有对象都会变成字符串。

用python打印的一行:

'2013-08-31 00:00:00', '', '1.0', '2013.0', '8.0', 'Q3','C', '03J', '', '',
 '', '', 'nan', 'nan', '', 'NON-AIRPORT', 'SELF-SERVICE', 'ICI', '', '19.0', '20130819', 
'1.0', '19.0', '9.0', '20130901', '2.0', '1.0', '1.0', '1.0', '10.0', '5.0', '5.0', '3.0',
 '4.0', '4.0', '2.0', '2.0', '', 'nan', '2.0', '', '24854524', 'nan', 'nan', 'nan', 'nan', 
'1.0', 'nan', '5.0', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 
'nan', '4.0', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 
'nan', 'nan', 'nan', '2.0', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 
'nan', '3.0', '5.0', '5.0'

正如您所看到的,所有日期,字符串,浮点数和整数都已转换为字符串。我怎样才能正确导入它们?假设我们有400列数据,我无法手动定义每列的类型。

2 个答案:

答案 0 :(得分:7)

你正在向后看。并不是说它们被转换为字符串,而是它们字符串,因为CSV不是保留类型信息的格式。你没有做任何事情把它们变成别的东西,Python也不会猜测。 Nan是一个漂浮物,还是一个祖母的亲切名字? 3.0是浮动的,还是前卫的nerdcore蓝调乐队的名字?

如果您能想出一种算法来猜测类型,那么您可以应用它,当然:

import csv
import ast
import datetime

def guess_type(x):
    attempt_fns = [ast.literal_eval,
                   float,
                   lambda x: datetime.datetime.strptime(x, 
                                                    "%Y-%m-%d %H:%M:%S")
                   ]
    for fn in attempt_fns:
        try:
            return fn(x)
        except (ValueError, SyntaxError):
            pass
    return x

with open("untyped.csv", "rb") as fp:
    reader = csv.reader(fp)
    for row in reader:
        row = [guess_type(x) for x in row]
        print row
        print map(type, row)

使用文件

2013-07-31 00:00:00,,1.0,2013.0,7.0,Q3,21160742,32HHBS1307170203,nan

以上代码将生成

[datetime.datetime(2013, 7, 31, 0, 0), '', 1.0, 2013.0, 7.0, 'Q3', 21160742, '32HHBS1307170203', nan]
[<type 'datetime.datetime'>, <type 'str'>, <type 'float'>, <type 'float'>, <type 'float'>, <type 'str'>, <type 'int'>, <type 'str'>, <type 'float'>]

这不错。

PS:如果您要在Python中使用CSV文件进行认真的工作,我强烈建议您查看pandas - 否则您将浪费时间重新实现其部分功能。

答案 1 :(得分:3)

它们不会转换为字符串,它们已经是字符串开头。但是你可以尝试在阅读后将它们转换成浮点数:

假设row包含一行数据,那么您可以

newrow = []
for item in row:
    try:
        newrow.append(float(item))
    except ValueError:
        newrow.append(item)