1.从CSV中导入数据
import csv,sys filename = 'ch02-data.csv' data = [] try: with open(filename) as f: reader = csv.reader(f) header = next(reader) data = [row for row in reader] except csv.Error as e: print("Error reading CSV file at line %s: %s" % (reader.line_num, e)) # sys.exit(-1) if header: print(header) print('==================') for datarow in data: print(datarow)
注意:python2和python3中next方法以变,python2是reader.next(),python3是next(reader)。
https://www.cnblogs.com/hfdkd/p/7719134.html
工作原理
补充说明
import numpy as np filename = 'ch02-data.csv' data = np.loadtxt(filename, dtype='str', delimiter=',') print(data)
2.从Excel中导入数据
import xlrd ## 专门读取 excel 的 package from pprint import pprint file = r"ch02-xlsxdata.xlsx" wb = xlrd.open_workbook(filename=file) # 读取 excel 文件,返回 xlrd.book.Book 类的实例对象 ws = wb.sheet_by_name("Sheet1") # 根据 sheet name 读取 sheet 中的数据,返回 xlrd.sheet.Sheet 类的实例对象 data_set = [] for r in range(ws.nrows): # sheet 的行数 col = [] for c in range(ws.ncols): # sheet 的列数 col.append(ws.cell(r, c).value) # 获取指定的 Cell 的实例对象,通过 value 属性获取值 data_set.append(col) pprint(data_set) for i in data_set: print(i)
工作原理
注意:python的pprint模块。https://blog.csdn.net/iamaiearner/article/details/9378093
补充说明
使用pandas从Excel中导入数据更加方便
import pandas as pd import numpy as np np.set_printoptions(linewidth=2000) filname = 'ch02-xlsxdata.xlsx' data = pd.read_excel(filname, header=None) print(data.values)
pandas读取Excel更多内容https://www.cnblogs.com/xinmomoyan/p/10875792.html