繁体   English   中英

如何从csv文件创建列表并将列强制转换为int和float

[英]How to create lists from csv files and cast columns to ints and floats

这是我现在用于读取两个不同CSV文件的功能:

def readCSVfile(str1):
    if str1 == 'small':
        with open('small.csv', 'r+') as f:
            read_small = csv.reader(f)
            for row in read_small:
                return read_small
    elif str1 == 'big':
        with open('big.csv', 'r+') as f:
                read_big = csv.reader(f)
                for row in read_big:
                    return read_big    
    else:
        print "File not found"

打印的小文件如下所示:

要将字符串转换为int和float分别使用int()float()方法。 演示可能如下所示:

lst = ['1', '1', '2.2', '1.3', '9.6']
[int(x) if "." not in x else float(x) for x in lst]

输出:

[1, 1, 2.2, 1.3, 9.6]

可能的代码修改:

def readCSVfile(str1):
    if str1 == 'small':
        with open('small.csv', 'r+') as f:
            read_small = csv.reader(f)
            next(read_small) # skip header
            modified_data = []
            for row in read_small:
                temp = [[int(x) if "." not in x else float(x) for x in row] # convert string elements to int and float elements
                modified_data.append(temp)
            return modified_data
    # ...

Ps在下面的简单示例中显示了将字符串转换为int或float的更强大的方法:

def num(s):
    try:
        return int(s)
    except ValueError:
        return float(s)

为了使用它,用temp修改字符串:

temp = [num(x) for x in row]

您可以轻松修改函数num()以在数据变脏(例如“ 1” num()情况下工作 或者是其他东西。

如果您愿意安装Pandas,它将完全满足您的期望:

>>> import pandas as pd
>>> data = pd.read_csv('test.csv')
>>> data.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 12 entries, 0 to 11
Data columns (total 5 columns):
site          12 non-null int64
experiment    12 non-null int64
length        12 non-null float64
width         12 non-null float64
height        12 non-null float64
dtypes: float64(3), int64(2)
memory usage: 576.0 bytes
>>> print(data)
    site  experiment  length  width  height
0      1           1     2.2    1.3     9.6
1      1           2     2.1    2.2     7.6
2      1           3     2.7    1.5     2.2
3      2           1     3.0    4.5     1.5
4      2           2     3.1    3.1     4.0
5      2           3     2.5    2.8     3.0
6      3           1     1.9    1.8     4.5
7      3           2     1.1    0.5     2.3
8      3           3     3.5    2.0     7.5
9      4           1     2.9    2.7     3.2
10     4           2     4.5    4.8     6.5
11     4           3     1.2    1.8     2.7

暂无
暂无

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

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