在python语言中,用丰富的函数库来从文件中提取数据,这篇博客讲解怎么从csv, xls文件中得到想要的数据。

点击下载数据文件http://seanlahman.com/files/database/lahman-csv_2015-01-24.zip

这个一个美国棒球比赛的统计数据
解压文件夹,我们选取AwardsManagers.csv来练习

#-*- coding:utf-8 -*-
import csv
DIR = 'data/'
fname = 'AwardsManagers.csv'
fpath = DIR+fname ## 用 with open() as filename 的结构非常优美, 而且不需要写代码来关文件
## 省去了fileobj.close(), 省去写try-finally的麻烦来出来exception with open(fpath, 'rb') as csvfile:
## delimiter是csv文件每行中数据间隔开的符号,常用是comma逗号,
## quotechar之间包括特殊字符
mreader = csv.reader(csvfile, delimiter=',', quotechar='|') ## 读出每一行都是一个list
first_row = mreader.next()
print first_row
print type(first_row)
## 目前的行数
print mreader.line_num
for row in mreader:
print ', '.join(row) ## 另外一个读取数据的方法是用DictReader
names = ['playerID','awardID','yearID','lgID','tie','notes']
with open(fpath) as csvfile:
## fieldnames指明了csv文件的列名称
reader = csv.DictReader(csvfile, fieldnames=names,
delimiter=',', quotechar='|')
for row in reader:
## 每一行都是一个dict对象
print(row[names[0]], row[names[1], row[names[2])

从专业机构中获取的数据也常常是XLS文件,用python提取XLS文件中的函数是xlrd

在xlrd中最重要的函数是:
xlrd.open_workbook
workbook.sheet_by_name
workbook.sheet_by_index
sheet.cell(row_index, col_index)
cell.value
sheet.col_values(col_index, start_row_index, end_row_index)
sheet.row_values(row_index, start_col_index, end_col_index)
sheet.col_slice(col_index, start_row_index, end_row_index)
sheet.row_slice(row_index, start_col_index, end_col_index)
点击下载数据源文件http://www.abs.gov.au/AUSSTATS/subscriber.nsf/log?openagent&33010do001_2009.xls&3301.0&Data%20Cubes&861A1F351DF2D978CA2577CF000DF18E&0&2009&03.11.2010&Latest
文件是关于澳大利亚人口出生情况的统计数据

#-*- coding:utf-8 -*-
import xlrd DIR = 'C:/Users/Lucas/Downloads/'
fname = '33010do001_2009.xls' # 首先建立workbook
mworkbook = xlrd.open_workbook(DIR+fname) # 打印出所有sheetnames
sheet_names = mworkbook.sheet_names()
print('Sheet Names', sheet_names) # 选取第二个sheet
msheet = mworkbook.sheet_by_name(sheet_names[1]) # 或者通过index得到sheet
nsheet = mworkbook.sheet_by_index(1)
print ('Sheet name: %s' % nsheet.name) # Pull the first row by index
row = msheet.row(0) # Pull the first row by index
row = msheet.row(4)
# Print 1st row values and types
for cell in row:
print cell.value # Print all values, iterating through rows and columns
#
num_cols = msheet.ncols # Number of columns
num_rows = msheet.nrows # Number of rows
for row_idx in range(0, num_rows): # Iterate through rows
row_values = []
for col_idx in range(0, num_cols): # Iterate through columns
row_values.append([msheet.cell(row_idx, col_idx).value]) ## 输出每行数据
print row_values ## 用col_slice得到某一列的数据
col_cells = msheet.col_slice(2, 4, num_rows)
for cell in col_cells:
print("-"*6)
print cell.value ## 用col_valeus得到某一列的数据
col_values = msheet.col_values(2, 4, num_rows)
print col_values

最新文章

  1. 【代码笔记】iOS-圆角矩形
  2. C++是一把很奇怪的刀
  3. [译]git clean
  4. windows2008安装IIS
  5. HTML5中的localStorage用法
  6. PAT 1002
  7. mvc3.0中[ValidateInput(false)]失效的问题
  8. spring mvc 资源包的映射
  9. Cocos2d-x 添加iOS7默认分享/AirDrop
  10. iOS https请求 NSURLSessionDataTask
  11. Spring.Net实现跨数据库服务层事务管理
  12. C# 线程手册 第三章 使用线程 实现一个数据库连接池(实战篇)
  13. 通过本地yum源安装软件报错[Errno 14] PYCURL ERROR 56 - "Failure when receiving data from the peer"
  14. 深度访谈Amazon员工与HR:华裔因pip跳楼背后(图)
  15. Jmeter 抓app包 抓到一半不好用了
  16. swoole_table应用类
  17. mysql-5.7中的innodb_buffer_pool_prefetching(read-ahead)详解
  18. 我们为什么要在 PHPStorm 中标记目录
  19. 通过SSH服务登陆linux服务器(版本RHEL7)
  20. 简单深搜:POJ1546——Sum it up

热门文章

  1. Docker学习总结
  2. Linux Hadoop2.7.3 安装(单机模式) 一
  3. scheduleInRunLoop作用
  4. 学习ASP.NET Core,你必须了解无处不在的“依赖注入”
  5. CRL快速开发框架系列教程一(Code First数据表不需再关心)
  6. 无限循环轮播图之运动框架(原生JS)
  7. HTML光标样式
  8. UWP简单示例(二):快速开始你的3D编程
  9. java设计模式之简单工厂模式
  10. [转载]iOS 10 UserNotifications 框架解析