Skip to content

Python Code Snippets

WBowam edited this page Apr 3, 2015 · 6 revisions

Date: 2015-03-30
Title: 代码片段
Tags: Snippets, Python
Category: IT

python logging

import logging  
logging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'), level = logging.DEBUG)  
logging.debug('this is a message')  

python get line num

f = open("test.txt")
for i, l in enumerate(f):
    print i

python 脚本基本结构

import sys

source_file = sys.argv[1]

if __name__ == "__main__":
     my_func(source_file)

python file

f = open(file, "a") ## a meaning append
f = open(file, "w") ## w meaning write

python 正则

import re
pattern = re.compile("[a-zA-Z]")
result = pattern.search("adsfuhefdfkj435")
print result.group()

python 下载静态文件,如,mp3

import urllib
urllib.urlretrieve(audio_url, "name.mp3")

升级pip

easy_install -U pip

Clone this wiki locally