使用Python处理Excel表格
http://cocobear.info/blog/2009/01/16/use-python-deal-with-excel/
使用Python处理Excel表格 2009年01月16日
给俺的boss写的一个小工具,使用Python对Excel进行统计,然后把结束生成一个新的Excel表格,使用到了xlrd和pyExcelerator两个库。
简单的介绍一下这两个库,先说xlrd,这个库读Excel比较方便,各种方法使用起来也挺方便:
bk = xlrd.open_workbook('your.xls')
sh = bk.sheets()[-1]
上面两句就可以打开Excel表格中的一个sheet,sheets得到的是一个list,存放所有的sheet。
sh.nrows是该sheet中的行数,知道这个后就可以使用for循环来读所有的单元格了:
sh.row(i)[3]这个就代表第i行的第4列。
再看看pyExcelerator,这个用起来有点别扭:
sheets = parse_xls('result.xls')
先打开一个表格,sheets是一个list,包含了所有表格的内容,每一项就是一个sheet,而每个sheet是二元tuple,第一个是该sheet的名字,第二个是一个dict,dict的key是一个二元组,表示单元格的坐标,如(0,0),第一行第一列。
从上面的分析中可以得出要访问Excel中第一个sheet的第一行第一列元素需要:
sheets[0][1][(0,0)]
pyExcelerator也不能得到行列数。
写文件也比较简单:
wb = Workbook()
ws = wb.add_sheet('result')
ws.write(0,0,“hello”)
wb.save('result.xls')
就不解释了:-)
写文件时需要注意直接写Unicode内容进去,而不要写编码过的内容。
本文来源于可可熊的窝 http://cocobear.info/blog , 原文地址: http://cocobear.info/blog/2009/01/16/use-python-deal-with-excel/
相关文档:
先说python
python的random模块提供了多个伪随机数发生器,默认都是用当前时间戳为随机数种子。
下面是该模块几个最常用的函数
random() Return the next random floating point number in the range [0.0, 1.0).
randint(a,b) Return a random integer N such that a <=
N <= b
randrange([star ......
#使用类
class CPerson:
#类变量好比C++中的静态成员变量
population = 0
def SayHi(self):
print('Hello World')
def HowMany(self):
if CPerson.population == 1:
print('I am the only person here.')
else:
print(('We have %d perso ......
写了个图片蜘蛛人玩玩,抓了几个网页试试,感觉不不错。核心的代码可能20行也不到,简洁明了,嘻嘻。废话少说,翠花,上代码~~
#coding=utf-8
import os
import sys
import re
import urllib
URL_REG = re.compile(r'(http://[^/\\]+)', re.I)
IMG_REG = re.compile(r'<img[^>]*?src=([ ......
还记得是一个月的事情,神奇般的在youtube上搜索python,有个老外的教程里面有这么个内容:
#=============================
## python 魔法传值
#=============================
#-*-coding:utf-8-*-
class sono:
def Dict(self,**args):
ret ......