分享

Python批量修改mdb名称

 昵称QAb6ICvc 2013-01-31

Python批量修改mdb名称

2012/06/10 16:30:27 已有 96人浏览

手头上有一批数据,由于采集的时候是按照模板生产的,需要将其中的mdb改成X_XXXX_XX_XX_*X的形式,数据文件的组织方式如下:

需要达到的效果很简单,如下图:

用Python试了一下,初步解决了问题,但是不甚完美,但是还是需要修改,有时间再修改下:

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
# -*- coding:gbk -*-
import os,shutil
  
dics={'5000':'H','10000':'G'}
  
def changeMdbName(inpath,outpath):
    for fdir in os.listdir(inpath):
        newfilen=returnNewName(fdir,outpath)
        oldpath=inpath+os.sep+fdir+os.sep+'model.mdb'
        newpath=outpath+os.sep+newfilen+'.mdb'
        shutil.copy(oldpath,newpath)
        print newpath
          
def returnNewName(filename,outpath):
    newfn=''
    for dic in dics.keys():
        if (filename.find(dic)<>-1):
            indentification=dics[dic]
            newfn=filename.replace(dic,'')
            newfn=addZero(newfn)
            newfn=newfn.replace('-','_')
    newfn=indentification+'_'+newfn
    if(newfn.endswith('_')):
        newfn=newfn[:-1]
    if(len(newfn)>23):
        myfile.write(newfn+' length:'+str(len(newfn))+'n')
        newfn='A'+newfn
    return newfn
  
def addZero(fn):
    nfn=''
    fns=fn.split('-')
    for f in fns:
        if(len(f)==1):
            index=fns.index(f)
            fns[index]='0'+fns[index]
    for f in fns:
        nfn=nfn+f+'-'
    return nfn
      
      
inputpath=r"C:data最终成果数据(8月1日)mdb合并数据5千mdb"
outputpath=r"C:datamdb5000mdb"
  
txtpath=outputpath+os.sep+'log_changeMdbName.txt'
myfile=open(txtpath,'w')
myfile.write("length must <=23,because when add'_DGXJQX',Feature name must <=30n")
changeMdbName(inputpath,outputpath)
myfile.close()
print "Done"

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多