Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Python 使用 xlrd/xlwt 读写 Excel 表格

$
0
0

平时经常会遇到处理 Excel 表格数据的情况,人工处理起来实在是太麻烦了,我们可以使用 python 来解决这个问题,我们需要两个 Python 扩展, xlrd 和 xlwt 。

使用 xlwt 写入 Excel 数据 xlwt 的安装方式 $ sudo pip install xlrd Sample import xlwt xls = xlwt.Workbook() sheet = xls.add_sheet('sample') sheet.write(0, 0, 'netcon') sheet.write(0, 1, 'conw.net') xls.save('sample.xls')

这个是一个最简单的例子,创建一个 Excel 表格,新建一个名为 sample 的 sheet ,并在 A1 、 B1 的位置写上 netcon 、 conw.net 。

使用 xlrd 读取 Excel 数据 xlrd 的安装方式 $ sudo pip install xlrd Sample import xlrd xls = xlrd.open_workbook('sample.xls') sheet = xls.sheets()[0] values = sheet.row_values(0) print(values)

这份代码使用 xlrd 读取上面创建的 Excel 表格,输出是:

['netcon', 'conw.net']

Viewing all articles
Browse latest Browse all 9596

Trending Articles