博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python比excel好在哪_在数据分析方面,比起python,excel的局限性在哪(python excle 图表)...
阅读量:4679 次
发布时间:2019-06-09

本文共 2385 字,大约阅读时间需要 7 分钟。

求助用python从数据库取数据动态生成表格的方法

一、可使用的第三

python中excel表格,常用的库有xlrd(读excel)表、xlwt(写excel)表、openpyxl(可读写excel表)等。xlrd读较大的excel表时效率高于openpyxl,所以我在写脚本时就采用了xlrd和xlwt这两个库。介绍及下载地址为:http://www.python-excel.org/ 这些库文件都没有提供修改现有excel表格内容的功能。一般只能将原excel中的内容读出、做完处理后,再写入一个新的excel文件。

二、常见问题

使用python处理excel表格时,发现两个个比较难缠的问题:unicode编码和excel中记录的时间。

因为python的默认字符编码都为unicode,所以打印从excel中读出的中文或读取中文名的excel表或sheet时,程序提示错误UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)。这是由于在windows中,中文使用了gb2312编码方式,python将其当作unicode和ascii来解码都不正确才报出的错误。使用VAR.encode('gb2312')即可解决打印中文的问题。(很奇怪,有的时候虽然能打印出结果,但显示的不是中文,而是一堆编码。)若要从中文文件名的excel表中读取数据,可在文件名前加‘u’表示将该中文文件名采用unicode编码。

有excel中,时间和日期都使用浮点数表示。可看到,当‘2013年3月20日’所在单元格使用‘常规’格式表示后,内容变为‘41353’;当其单元格格式改变为日期后,内容又变为了‘2013年3月20日’。而使用xlrd读出excel中的日期和时间后,得到是的一个浮点数。所以当向excel中写入的日期和时间为一个浮点数也不要紧,只需将表格的表示方式改为日期和时间,即可得到正常的表示方式。excel中,用浮点数1表示1899年12月31日。

三、常用函数

以下主要介绍xlrd、xlwt、datetime中与日期相关的函数。

import xlrd

import xlwt

from datetime

def testXlrd(filename):

book=xlrd.open_workbook(filename)

sh=book.sheet_by_index(0)

print "Worksheet name(s): ",book.sheet_names()[0]

print 'book.nsheets',book.nsheets

print 'sh.name:',sh.name,'sh.nrows:',sh.nrows,'sh.ncols:',sh.ncols

print 'A1:',sh.cell_value(rowx=0,colx=1)

#如果A3的内容为中文

print 'A2:',sh.cell_value(0,2).encode('gb2312')

def testXlwt(filename):

book=xlwt.Workbook()

sheet1=book.add_sheet('hello')

book.add_sheet('word')

sheet1.write(0,0,'hello')

sheet1.write(0,1,'world')

row1 = sheet1.row(1)

row1.write(0,'A2')

row1.write(1,'B2')

sheet1.col(0).width = 10000

sheet2 = book.get_sheet(1)

sheet2.row(0).write(0,'Sheet 2 A1')

sheet2.row(0).write(1,'Sheet 2 B1')

sheet2.flush_row_data()

sheet2.write(1,0,'Sheet 2 A3')

sheet2.col(0).width = 5000

sheet2.col(0).hidden = True

book.save(filename)

if __name__=='__main__':

testXlrd(u'你好。xls')

testXlwt('helloWord.xls')

base=datetime.date(1899,12,31).toordinal()

tmp=datetime.date(2013,07,16).toordinal()

print datetime.date.fromordinal(tmp base-1).weekday()

在数据分析方面,比起python,excel的局限性在哪

如下:

1、Python是一门编程语言,多用它写出来的,也有直接对excel操作的Python工具包。

2、excel只是个表格处理工具,虽然里面也可以编程。

3、简单来说excel能做到的Python都能做到,但是excel不能很强大的编程,不能进行丰富逻辑处理,复杂的运算分析

用Python生成excel,如何用Python实现excel下单菜单的功能

不贴代码了,否则我这个回复,又被系统过。

自己用google搜:

【已解决】Python中处理操作Excel中的图表(Chart,Graph)

就可以找到我的帖子了。

里面有详细的代码和总结。

(此处不给贴地址,请自己用google搜标题,就可以找到帖子了)

如何用python创建excel表格

python可以用openpyxl包来读取和写入xlsx文件。

转载地址:http://ptfkp.baihongyu.com/

你可能感兴趣的文章
angularjs 中使用 service 在controller 之间 share 对象和数据
查看>>
禁止在 .NET Framework 中执行用户代码。启用 "clr enabled" 配置选项
查看>>
JSON、闭包和原型----透视Javascript语言核心
查看>>
[苹果]苹果AppStore应用审核标准
查看>>
lxr看代码的时候出现中文乱码问题
查看>>
CImageList使用指南(转)
查看>>
常量like数据库表中的列
查看>>
VC2012编译CEF3-转
查看>>
Log4net的配置-按照日期+文件大小混合分割
查看>>
const char*、char*、char* const、char[]、string的区别
查看>>
『cs231n』绪论
查看>>
SQL学习笔记:基础SQL语句
查看>>
python管理网络设备的一些模块
查看>>
VirtualProtect、VirtualLock、VirtualUnlock
查看>>
Stl
查看>>
mysql在windows下主从同步配置
查看>>
webqq 获得好友列表hash算法 获得最新hash的方法
查看>>
CSS实现强制换行-------Day 78
查看>>
Python批量删除指定目录下的指定类型的文件
查看>>
Machine Learning #Lab1# Linear Regression
查看>>