目前接触到的以下三种方法:
1.numpy (import numpy as np)
read:
file = np.loadtxt('input_file.csv/txt', skiprows=1, dtype=person_type)#将数据加载到数组,skiprows=1跳过标题行,person_type=dtype([('col_name1', 'type1'), ('col_name2', 'type2')]),可指定没列的数据类型
file = np.genfromtxt('input_file.csv/txt', dtype=float, names=True, converters={colunm_index:convert_func1, column_index:convert_func2})#dtype设置数值类型,names表示genfromtxt应该将第一行作为列标题,converters设置了一个字典,将列值映射到一个函数,函数会对列中的值进行转换
write:
savetxt('output_file.csv/txt', fmt='%.2f', delimiter=',', comments=' ', header=header_string)#savetxt默认使用科学技术形式保存数据,fmt可以设置保存数据的形式,delimiter设置列分隔符,savetxt默认不保存标题行,使用header参数提供一个字符串,设置标题行,savetxt默认在第一个列标题前面加#,以使这一行成为注释,可通过comments参数设为空字符串取消这个设置
2.pandas (import pandas as pd)
file = pd.read_csv('input_file.csv')
3.csv (import csv)