代码 import struct import ctypes def test_struct(buf, offset): return struct.unpack_from("I", buf, offset)[0] def test_ctypes(buf, offset): return ctypes.c_uint32.from_buffer(buf, offset).value def test
去掉添加到最终的结果集中,直到两个子序列归并完成。 代码如下: #!/usr/bin/python import sys def merge(nums, first, middle, last):
相比于Django而言,bottle显得非常轻量级。短短几行代码即可快速搭建一个简易的http server。提供了 Python Web开发中需要的基本支持:URL路由,Request/Response对象封装,模板支持,
Python识别验证码 #encoding=utf-8 import Image,ImageEnhance,ImageFilter import sys image_name = "./1.jpeg"
通过调用GetSystemPowerStatus Windows Api来判断AC Power是开启还是关闭状态。是一个python调用windows api的例子。 import os import win32con import
http://pymssql.sourceforge.net/ 下载pymssql模块,必须安装这个模块才可以用python连接mysql 以下是简单的访问代码,需要注意字符集 # -*- coding:gbk -*-
如果下载小文件,可以直接使用urllib.urlretrieve方法: import urllib urllib.urlretrieve ("http://www.example.com/songs/mp3.mp3", "mp3.mp3") 但是如果下载的文件尺寸很大,就不适合直接urlretrieve了。我们需要按块读取文件来下载: import urllib2 url = "http://do
使用 MySQLdb 模块连接 MySQL # coding=utf-8 #引入mysql python客户端模块 import MySQLdb import sys #进行数据库连接 conn = MySQLdb
下面的函数封装了系统的grep功能,传递文件名和参数返回grep的结果 import subprocess def grep(filename, arg): process = subprocess.Popen(['grep', '-n', arg, filename], stdout=subprocess.PIPE) stdout, stderr = process.communicate()
本代码可以帮你自动剪切掉图片的边缘空白区域,如果你的图片有大片空白区域(只要是同一颜色形成一定的面积就认为是空白区域),下面的python代码可以帮你自动切除,如果是透明图像,会自动剪切大片的透明部分。 本代码需要PIL模块 import
/usr/bin/env python # # [SNIPPET_NAME: Dictionaries 101] # [SNIPPET_CATEGORIES: Python Core] # [SNIPPET_DESCRIPTION:
parseFile(open("sample.xml")) parser.close() 7.python的GUI模块标准的是Tkinter,也有QT和MFC的模块,有兴趣的大家自己搜索下 import
Caesar算法是最简单的加解密算法... # Caeser Cipher import sys,os MyCypher = 25 MyDict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz `1234567890-=~!@#$%^&*()_+[]\\;\',./{}|:"<>?' plaintext = 'Hello World!
##不知道干啥用的 ws.cell('A2').style.alignment.wrap_text =True ##背景 好像不太好用 是个BUG ws.cell('A2').style.fill.fill_type =Fill
1.冒泡排序 比较相邻的元素大小,将小的前移,大的后移,就像水中的气泡一样,最小的元素经过几次移动,会最终浮到水面上。 def bubble(list): for i in range(len(list)): for j in range(0,len(list)-1-i): if list[j] > list[j+1]: list[j],list[j+1]=list[j+1],list[j] if
''''' Bubble Sort @author: aihua.sun ''' import string import random number = []; count = 0; def bubbleSort(): #initializeNumberArray(); generateRandomNumberArray(10, 1, 300); showArray(); sort(); sho
import sys, urllib2, urllib def addGETdata(url, data): '''Adds data to url. Data should be a list or tuple consisting of 2-item lists or tuples of the form: (key, value). Items that have no key should
import sys, socket hostname = socket.gethostname() # Try to get the fully-qualified name. print 'Fully-qualified name:', socket.getfqdn(hostname)
需要安装python Crypto: #!/usr/bin/env python # -*- coding: utf-8 -*- from Crypto.Cipher import AES import
from hashlib import md5 def md5_file(name): m = md5() a_file = open(name, 'rb') #使用二进制格式读取文件内容 m.update(a_file.read()) a_file.close() return m.hexdigest() if __main__ == '__init__': print md5_file('/h