-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
executable file
·179 lines (148 loc) · 4.1 KB
/
temp.py
File metadata and controls
executable file
·179 lines (148 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
"""
Spyder Editor: LTT
This is a temporary script file.
"""
"""
#%% open操作函数1
def openNew(inputfile,outputfile):
fh = open(inputfile)
out = open(outputfile,'w')
for line in fh.readlines():
one = line.split("\t")
one[0] = one[0].replace("chr","")
out.write('__'.join(one)+'\n') ##写出文件
fh.close()
out.close()
openNew('D:/Learn/Python/test/test.txt','D:/Learn/Python/test/out.txt')
# open操作函数2
def openNew(inputfile,outputfile):
fh = open(inputfile)
out = open(outputfile,'w')
while 1:
line1 = fh.readline()
if line1 != "":
#print(line1)
one = line1.strip().split("\t")
one[0] = one[0].replace("chr","")
out.write('__'.join(one)+'\n')
else:
break
fh.close()
#out.close()
openNew('D:/Learn/Python/test/test.txt','D:/Learn/Python/test/out2.txt')
#%% 读取文件行数
count=len(open('D:/Learn/Python/test/out.txt').readlines())
#%% 自定义函数1
def abc(a,b,c):
return a*100+b*10+c
print(abc(11,12,13))
#自定义函数2
def new(s):
res=[]
for i in range(len(s)):
#print(i)
res.append(s[i])
res.append("**")
return(res)
print(new("ababab"))
#%% which function
def indexNew(list1,ba):
start = 0
index = []
for i in range(len(list1)):
res = list1.find(ba,start)
if res == -1:
break
index.append(res)
start = res+1
return(index)
list1="abababssat"
print(indexNew(list1,"ab"))
#which function2
import re
list1="abababssat"
index = [m.start() for m in re.finditer("ab", list1)]
print(index)
#group()返回被 RE 匹配的字符串
#start()返回匹配开始的位置
#end()返回匹配结束的位置
#span()返回一个元组包含匹配 (开始,结束) 的位置
#print(re.findall("a",list1))
#print(list1.index("a"))
#print(list1.find("a"))
#%% re.compile()
import re
pattern=re.compile('[a-zA-Z]') #get a pattern
res=pattern.findall('siHI2hj$e')
print(res) #['s', 'i', 'H', 'I', 'h', 'j', 'e']
#%% re.findall() and re.finditer()区别
re.findall(r'[a-z]',"siiIHKH34ss") #['s', 'i', 'i', 's', 's']
[m.start() for m in re.finditer(r'[a-z]',"siiIHKH34ss")] #[0, 1, 2, 9, 10]
[m.group() for m in re.finditer(r'[a-z]',"siiIHKH34ss")] #['s', 'i', 'i', 's', 's']
#%% range
range(1,5) #[1,2,3,4]不包括5
range(1,5,2) #[1,3]隔两个输出
#%% split
list1.split("a") #同re.split("a",list1)
#%% 仅在本脚本中运行的代码行,而在该脚本被调用时不运行
if __name__ == '__main__':‘
#%% 判断以XXX开头或结尾
if filename.startwith("xxx"):
#TRUE
if filename.endwith(".jpg"):
#TRUE
#%% 删除
del list1
del list1[0]
#%% join
'\t'.join("ssss")
#%% 判断一个list中是否有某个元素
def getSeq(seq,dic):
seqlist=list(seq)
res = []
for base in seqlist:
if base in dic:
res.append(dic[base])
if base not in dic:
res.append("N")
return res
dic = {"A":"T","T":"A","G":"C","C":"G"}
seq="ATCTGTGCSGATDG"
tt=getSeq(seq,dic)
print(tt)
#%% 判断是否存在某个路径或文件
import os
os.path.exists(r'C:\1.TXT')
#%% do nothing
if filename.startwith("XX"):
#TRUE
else:
pass
#%% get current time from Linux
import os
os.system('date')
#or
import time
time.localtime()
#%% Python调用shell
os.system(cmd) #返回值是脚本的退出状态码
os.popen(cmd) #返回值是脚本执行过程中的输出内容
os.system('date')
os.popen('date').read()
os.popen('ls /work1/xuelab/project/ |grep liutt').read()
#%% 抛出异常
import sys
sys.exit(0) #无错误退出
sys.exit(1) #有错误退出
#%% 外部传参
import sys
sys.argv #命令行参数List,第一个元素是程序本身路径
sys.argv[0] #即test.py所在路径,不用考虑
sys.argv[1] #arg1
sys.argv[2] #arg2
sys.argv[3] #arg3
...
#调用
python test.py arg1 arg2 arg3
"""