1. openpyxl / xlrd / xlwt  => 操作Excel 文件(xlsx格式)

  => xlrd + xlwt : 只能操作xls文件,分别负责读写, 暂时不讨论

  => openpyxl : 只能用来处理Excel2007及以上的版本, .xlsx/.xlsm文件

  读xlsx文件

#coding=utf-8
from openpyxl import load_workbook
wb = load_workbook(filename=r'a.xlsx') #加载workbook,也就是一个Excel文件
sheets = wb.get_sheet_names() #获取所有worksheet的名字
print "sheets: ",sheets
sheet0 = sheets[0] #得到第一个sheet的名字
ws = wb.get_sheet_by_name(sheet0) #如果sheet不存在不会抛出异常,而是返回None
#获取所有的行和列
rows = ws.rows
columns = ws.columns
content = []
# 双重循环获取sheet0的内容
for row in rows:
line = [col.value for col in row]
content.append(line) print content
#通过坐标读取值, 注意编号是从1 开始
print ws.cell('B12').value #Excel内部索引, 使用ws['B12'],应该也能读取,方法有很多种
print ws.cell(row=12,column=2).value #数组索引

创建xlsx文件,创建sheet表单

#coding=utf-8
from openpyxl import Workbook
# 创建一个workbook对象
wb = Workbook()
# 获取当前活动的worksheet
ws = wb.active
# 通过赋值给单元格来写
ws['A1'] = "roger"
# 可以写一整行
ws.append([1,2,34])
# python 的类型将会自动转换
import datetime
ws['A2'] = datetime.datetime.now()
# 最后一定要保存文件, 当然也可以在其他地方保存(创建之后)
wb.save("b.xlsx")
# 创建一个新的sheet
wb.create_sheet(title='roger')
wb.save('b.xlsx')

2.Queue

  是一个同步队列类,在线程安全的多线程环境中很适用。模块实现了所有required locking semantics,依赖于python对线程的支持!

  模块实现了3种类型的queue:

    FIFO: the first tasks added are the firsted retrieved.

    LIFO(like stack): the most recently added entry is the first retrieved.

    Priority queue: the entries are kept sorted (using the heapq mudule), the lowest valued entry is retrieved first.

  模块中定义的类和异常:

    Clssess:

      Queue.Queue (maxsize=0: mean infinite,下面的也都是)

      Queue.LifoQueue

      Queue.PriorityQueue

    Exceptions:

      Queue.Empty

      Queue.Full

  常用方法:

    Queue.qsize(): 返回queue的大小

    Queue.empty(),    Queue.full()

    Queue.put(item[,block[,timeout]]:  存放item,如果block 参数为true且timeout为None(default), block if necessary until a free slot is available.

    Queue.put_nowait(item) : 等同 Queue.put(item,False)

    Queue.get([block[,timeout]]): 删除并返回queue中对应的item。

    Queue.get_nowait(): 等同 Queue.get(False)

    Queue.task_done(): 指示以前的操作完成了,被Queue的消费者线程使用。对于每一个get()用来获取元素后,一个subsequent调用 task_done() 来告诉Queue任务处理完成。

    Queue.join(): Block until all items in the queue have been gotten and processed! 只有当调用task_done()之后未完成任务数才会减少,减少为0 的时候,join() unblocks.

伪代码

# 一段不能运行的sample
#coding=utf-8
from Queue import Queue
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue() # 如果只使用 import Queue, 那么这行需要用 Queue.Queue
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start() for item in source():
q.put(item) q.join() # block until all tasks are done

参考代码(参考原文链接)

#coding=utf-8
#
#FIFO
from Queue import Queue
q = Queue(0)
for i in range(10):
q.put(i)
while not q.empty():
print q.get() # LIFO
from Queue import LifoQueue
q = LifoQueue(maxsize=0)
for i in range(10,20):
q.put(i)
while not q.empty():
print q.get() # Priority Queue
from Queue import PriorityQueue
q = PriorityQueue() class work(object):
def __init__(self,priority,description):
self.priority = priority
self.description = description def __cmp__(self,other): #自定义比较函数
return cmp(self.priority, other.priority) q.put(work(4,"Middle range work"))
q.put(work(1,"Emergency work"))
q.put(work(7,"Low priority work")) while not q.empty():
wk = q.get()
print wk.priority,":",wk.description

3.Thread模块

  这个模块提供低级原语来使用多线程, 多线程共享他们的全局数据空间,从而实现同步, 提供简单的互斥锁。dummy_thread重复实现了这个模块,更适用,Threading是更高级的多线程实现。

  模块定义了如下常量和函数

  Exceptions:

    thread.error : Raised on thread-specific errors.

  Constants:

    thread.LockType: lock对象的类型

  Functions:

    thread.start_new_thread(function,args[,kwargs]): 开始一个新线程并返回他的 线程ID,这个线程执行函数 function,args是这个function的参数。

    thread.interrupt_main(): 在主线程中抛出一个keyboardInterrupt异常,一个子线程可以利用这个函数来中断主线程

    thread.exit(): 抛出一个SystemExit的异常, 如果不捕获的话就终止线程

    thread.allocate_lock() : 返回新的lock对象

    thread.get_ident(): 返回当前线程的id

    thread.stack_size([size]): 返回线程堆栈大小

    lock.acquire([waitflag]): 请求锁, 等待其他线程释放锁。

    lock.release()

    lock.locked(): 判断是否locked,返回True / False

    

import thread
a_lock = thread.allocate_lock()
with a_lock:
print "a_lock is locked while this executes"

Python 简单模块学习的更多相关文章

  1. python - argparse 模块学习

    python - argparse 模块学习 设置一个解析器 使用argparse的第一步就是创建一个解析器对象,并告诉它将会有些什么参数.那么当你的程序运行时,该解析器就可以用于处理命令行参数. 解 ...

  2. python paramiko模块学习分享

    python paramiko模块学习分享 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Sola ...

  3. Python logging 模块学习

    logging example Level When it's used Numeric value DEBUG Detailed information, typically of interest ...

  4. python logging模块学习(转)

    前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...

  5. Python time模块学习

    Python time模块提供了一些用于管理时间和日期的C库函数,由于它绑定到底层C实现,因此一些细节会基于具体的平台. 一.壁挂钟时间 1.time() time模块的核心函数time(),它返回纪 ...

  6. python os模块学习

    一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的. 二.常用方法 1.os.name 输出字符串指示正在使用的平台.如果是wi ...

  7. python atexit模块学习

    python atexit模块 只定义了一个register模块用于注册程序退出时的回调函数,我们可以在这个函数中做一下资源清理的操作 注:如果程序是非正常crash,或者通过os._exit()退出 ...

  8. Python 第二模块学习总结

    学习总结: 1.掌握对装饰器的用法 2.掌握生成器的用法 3.掌握迭代器的用法 4.熟悉Python内置函数 5.熟悉Python shutil/shelve/configparse/hashlib/ ...

  9. Python requests模块学习笔记

    目录 Requests模块说明 Requests模块安装 Requests模块简单入门 Requests示例 参考文档   1.Requests模块说明 Requests 是使用 Apache2 Li ...

随机推荐

  1. Maven构建项目速度太慢的解决办法 Maven 调试

    Apache Maven是当今非常流行的项目构建和管理工具,它把开发人员从繁杂的项目依赖关系处理事务中解放出来,完全自动化管理依赖问题.在Web应用开发过程中,通常我们会用到maven的archety ...

  2. Oracle清理大表,降水位

    背景:一张表的清理机制存在问题,导致该表的数据一直在增加,该表水位已很高,需要对该表的数据进行清理并降水位. 1.1 迁移前准备 步骤一.新建表 p_transaction_bak. oracle@l ...

  3. ubuntu下面搭建SolrCloud集群

    首先要先把ubuntu环境搭建好,配置好静态IP,我这边配置的是3台机子,solr搭建集群至少是2台. 192.168.0.15  主机 192.168.0.16  从机 192.168.0.17  ...

  4. c++ 先序构建二叉树

    二叉树首先要解决构建问题,才能考虑后续的遍历,这里贴出通过先序构建二叉树,同时包含四种二叉树的遍历方法(先序,中序,后序,逐层) 第一.定义BinaryTreeNode 类 #include < ...

  5. java 字符串排序

    http://bbs.csdn.net/topics/280032929 大可不需要那样复杂了!(一)如果要排序的为字符串,如:String sortStr = "ACDFE";  ...

  6. 0CTF题中的神奇宝贝WP

    前言: 今天被尘少拉去做题 然后做到一个脑洞打开的题..... 神奇宝贝!?!? 正文: 一开始来到这=-= 弱密码猜测:admin admin 验证码用calc去算 然后成功进入 第一次去做神奇宝贝 ...

  7. 3.docker学习之docker与虚拟化

    虚拟化技术是一个总称,是一系列实现虚拟技术的统称.从广义上来说,虚拟化技术包括了虚拟机技术和容器技术, 所谓虚拟化技术最大的特点就是将一个真实的机器进行虚拟地分割,然后分割出来的部分可以独立使用   ...

  8. [转] C#实现在Sql Server中存储和读取Word文件 (Not Correct Modified)

    出处 C#实现在Sql Server中存储和读取Word文件 要实现在Sql Server中实现将文件读写Word文件,需要在要存取的表中添加Image类型的列,示例表结构为: CREATE TABL ...

  9. Eclipse注释配置

    新的文件/** * @ClassName: ${type_name}  * @Description: ${todo} * @author ${user} * @date ${date} ${time ...

  10. kmp(详解)

    大佬博客:https://blog.csdn.net/lee18254290736/article/details/77278769 对于正常的字符串模式匹配,主串长度为m,子串为n,时间复杂度会到达 ...