2012年3月6日星期二

enumerate用法


  1. enumerate(sequence[, start=0]) )返回一个枚举对象,sequence必须是一个序列、迭代器或者具有具有迭代功能的对象。enumerate()是适用于获得索引集:(0, seq[0]), (1, seq[1]), (2, seq[2]), ....

  2. >>> i = 0  
  3. >>> seq = ['one''two''three']  
  4. >>> for element in seq:  
  5. ...     seq[i] = '%d: %s' % (i, seq[i])  
  6. ...     i += 1  
  7. ...  
  8. >>> seq  
  9. ['0: one''1: two''2: three']  
  10.   
  11.   
  12. >>> seq = ['one''two''three']  
  13. >>> for i, element in enumerate(seq):  
  14. ...     seq[i] = '%d: %s' % (i, seq[i])  
  15. ...  
  16. >>> seq  
  17. ['0: one''1: two''2: three']  
  18.   
  19.   
  20. >>> def _treatment(pos, element):  
  21. ...     return '%d: %s' %(pos, element)  
  22. ...  
  23. >>> seq = ['one''two''three']  
  24. >>> [_treatment(i, e1) for i, e1 in enumerate(seq)]  
  25. ['0: one''1: two''2: three']  

转自:http://greybeard.iteye.com/blog/1295827

2012年2月24日星期五

python+pyqt4脚本转exe程序


python+pyqt4脚本转exe程序
转换工具:Py2exe
运行平台:Windows

下面按步骤说明将一个Python脚本转换成exe程序的过程。
1. 安装Py2exe
2. 进入hello.py的目录,编写安装脚本setup.py。其内容如下:

1. 精简版+图标  pyqt4打包
#setup_pyqt.py
#with logo.ico and all the files bundled in one exe

name1=raw_input("Enter the name of py file: ")
from distutils.core import setup
import py2exe

setup(zipfile=None,
      windows=[{"script" : name1, 'icon_resources':[(1, 'logo.ico')]}],
      options={"py2exe" : {"includes" : ["sip"]},"py2exe":   { "bundle_files": 1,"includes" : ["sip"] }})



2. 精简版:打包 python 脚本 成 带图标的exe 程序,无小文件生成

#setup_python.py
#with logo.ico and all the files bundled in one exe

name1=raw_input("Name of py file: ")
from distutils.core import setup
import py2exe
import sys
includes = ["encodings", "encodings.*"]  
sys.argv.append("py2exe")
options = {"py2exe":   { "bundle_files": 1,"includes" : ["sip"] }    }
setup(options = options,
      zipfile=None,
      console = [{"script":name1, 'icon_resources':[(1, 'logo.ico')]}])


3. 打开DOS并切换其工作目录为当前目录,按如下方式在DOS中运行setup_pyqt.py, setup_python.py:

python setup_pyqt.py py2exe
python setup_python.py py2exe

2011年12月30日星期五

ID To Namelist


ID To Namelist

import os
import time, sys
from Bio import SeqIO

filename=raw_input("Pls enter the name of the output txt file of Blastall.exe: ")
Approach=raw_input("Pls enter the the approach of your Datebase: ")

# Timing start
start=time.time()
wrerr = sys.stderr.write


# Create a file named 'Results' in current dir
if os.path.exists('Results'):
    for root, dirs, files in os.walk('Results'):
        for name in files:
            os.remove(os.path.join(root,name))
else:
    os.mkdir('Results')

f=open(filename,'r')
f1=open('Results//ID_datebase.txt','w')

a={}
b=[]

# Search every dirs and files at F:\python missions\YY\Datebase
for root,dirs,files in os.walk(Approach):
    for file in files:
        for record in SeqIO.parse(os.path.join(root, file), 'fasta'):
            b.append(record.id)
        a[os.path.join(root, file).split('\\')[-2]+'-'+os.path.join(root, file).split('\\')[-1]]=b
        b=[]

# Output ID_datebase
f1.write('Namelist'+'\t'+'Corresponding ID'+'\n')
for key in sorted(a.keys()):
    f1.write(str(key)+'\t'+str(a[key])+'\n')

# Name the IDs
num=0

for line in f:
    num+=1
    for key in a.keys():
        if line.split('\t')[1].strip() in str(a[key]):
            f2=open('Results\\'+key+'.txt','a')
            f2.write(str(line).strip()+'\t'+key+'\t'+'\n')
    if num%100000==0:
        print str(num)+" sequences have been processed!"
   
end=time.time()
wrerr("OK, All Work Finished in %3.2f secs\n" % (end-start))
raw_input("Press <Enter> to close this window: ")





2011年11月10日星期四

用Python的split输出excel文件


Here’s an example of some data where the dates not formatted well for easy import into Excel:
20 Sep, 263, 1148,   0,   1,   0,   0,   1,   12.1,   13.9, 1+1, 19.9
 20 Sep, 263, 1118,   0,   1,   0, 360,   0,   14.1,   15.3, 1+1, 19.9
 20 Sep, 263, 1048,   0,   1,   0,   0,   0,   14.2,   15.1, 1+1, 19.9
 20 Sep, 263, 1018,   0,   1,   0, 360,   0,   14.2,   15.9, 1+1, 19.9
 20 Sep, 263, 0948,   0,   1,   0,   0,   0,   14.4,   15.3, 1+1, 19.9
The first column has the day and month separated by a space. The second column is year-day, which we’ll ignore. The third column has the time. The data we’re interested in is in the 9th column (temperature). The goal is to have a simple Excel file where the first column is date, and the second column is temperature.
以上从某博客上见到的采用xlrd模块来写excel,感觉很复杂。 自己也编写了一个python脚本来完成相同任务,如下所示:
f = open('weather.data.example.txt','r')
f1= open('result.xls','w')
for line in f:
    L = line.strip().split(',')
    if len(L) < 12:
        continue
    date = L[0].strip()
    time = L[2].strip()
    temperature = str(L[8])
    f1.write(date+'-'+time+'-'+'\t'+temperature+'\n')
print 'OK, finished'

利用list(set())和sorted处理列表或字典

1.利用list(set(a))来删除列表a或者字典中重复的元素。


>>> a=['2','2','4','3','4','0','1','1']
>>> list(set(a))
['1', '0', '3', '2', '4']


2.利用sorted 来排序字典a。排序原则:将列表b中元素按照其在字典a中对应值的大小来排序。Reference: http://wiki.python.org/moin/HowTo/Sorting/

>>> a={'a': '3', 'c': '2', 'e': '1', 'd': '0'}
>>> b=['a','e']
>>> sorted(b,key=a.__getitem__)
['e', 'a']


特殊地:
对字典a的 健值列表(a.keys()) 按其对应值的大小进行排序,结果输出到列表中

>>> a={'a':'3','c':'2','d':'0','e':'1'}
>>> sorted(a.keys(),key=a.__getitem__)
['d', 'e', 'c', 'a']