from dateutil import parser
a=parser.parse('20100101123145')
>>a=datetime(2010,1,1,12,31,45)
月度归档: 2021 年 12 月
python中datetime模块的strftime函数
from dateutil import parser
a=parser.parse('20100101')
如果想获得这个时间的字符串表达,则可用
a.strftime('%Y%m%d')
python中根据某个list排序另一个list
https://s tackoverflow.com
/questions/6618515/sorting-list-based-on-values-from-another-list
比如有如下两个list:
people =['Jim','Pam','Micheal','Dwight']
ages =[27,25,4,9]
按ages排序
方法一:
[x for _,x in sorted(zip(ages,people))]
方法二:
import numpy as np
people=np.array(people)
ages=np.array(ages)
indx=ages.argsort()
sortedpeople=people[indx]
方法三:
sorted_index=sorted(range(len(age)),key=lambda x:people[x])
[people[i] for i in sorted_index]
顺便鄙视一下百度和墙。
matplotlib显示TimesNewRoman
import matplotlib as mpl
mpl.rcParams['font.family']='serif'
mpl.rcParams['font.serif']='Times New Roman'
pandas中的to_datetime
使用这个函数的时候要保证dataframe的列名有year,month,day,hour,minute,second这样的,缩写成yr,mm,dy这样的都不行。
移动pandas的dataframe中不同列的位置
将一个hypodd格式的目录读入进了dataframe,想弄成年、月、日这样的顺序,涉及到重新排列columns的位置,网上建议这么操作newcata=cata[[‘year’,’month’,’day’]]
seisan生成IASP91_linux.HED
In order to generate the earth model les IASP91.HED and IASP91.TBL, first run program REMODL,then program SETBRN. The program REMODL has the earth model hardwired. Note: These binary lesCANNOT be moved between platforms. They are included with SEISAN for each respective distribution.If lost, they must be regenerated on the same platform
python与shell交互
之前用到了subprocess模块,但有人提出可能会导致阻塞,最近在网上看到了pexpect模块,操作起来就已经跟perl很像了。
import pexpect
child=pexpect.spawn("sac")
child.sendline("r test.sac")
child.sendline("div 100")
child.sendline("w over")
child.sendline("q")
python多任务,线程池
25个线程要做完1000个任务
import threading,time,random,os,threadpool
from shutil import copyfile
def dealing(cata):
print threading.current_thread().name
print 'entering ',cata
os.popen('sh DORFTN '+cata).read()
# os.chdir('..')
if __name__ == "__main__":
cata=[c for c in os.listdir('.') if os.path.isdir(c)]
pool=threadpool.ThreadPool(25)
requests=threadpool.makeRequests(dealing,cata)
[pool.putRequest(req) for req in requests]
pool.wait()
如果函数内有多个参数,可如下处理:
import threadpool, os, glob
out=[]
def processing(sample,trace):
tmp=sample.split('/')
tracefile=trace+'/'+tmp[-1]
ret=os.popen('./doublet_search %s %s' % (sample,tracefile)).read()
print(ret)
if __name__=='__main__':
traces=glob.glob('Trace/2*')
samples=glob.glob('Template/2*')
arglist=[]
for sample in samples:
Zs=glob.glob(sample+'/*.?HZ')
for Z in Zs:
sam=Z[0:-1]
for trace in traces:
arglist.append(([sam,trace],None))
# print(arglist)
pool_t=threadpool.ThreadPool(8)
requests=threadpool.makeRequests(processing,arglist)
[pool_t.putRequest(req) for req in requests]
pool_t.wait()
****注:一般在函数中不要用到os.chdir,因为chdir是全局的,如果要用,则要用绝对路径。
numpy数组数据更改
a[:,2:4]=10
a[a>5]=10
np.where(a>5,a,10) # if a>5, values equal to a themselves, else equal to 10
a.clip(3,10) # if a<3, =3, if a<10, =10