将清单中的"元件值\t元件位号"分裂为"元件位号\t元件值",每个元件一行

来源:互联网 发布:java面试题及答案2016 编辑:程序博客网 时间:2024/05/22 15:51

在BOM中,清单的格式一般有物料编码,物料描述,元件位号,元件数量,将物料描述简化成一个值,再将这列与元件位号列提取出来,保存到"value+refs.txt"文件中,格式为

元件值\t位号1 位号2 ... 位号n    位号之间的分隔符为1个空格

将下面的脚本复制,保存为"reflist.py"文件,与"value+refs.txt"文件放在同一目录中,操作系统中要先安装有python2(python3未验证),运行此脚本,就会将"value+refs.txt"中的元件按第行一个的格式排列,输出"reflist.txt"文件,此文件就可以直接用作loadBomValue2PCB.bas的输入,将清单中的值导入到PCB中


#!/usr/bin/pythondebug=1logfile="./python_debug.txt"#将"C11-14"转换成"C11,C12,C13,C14"def expandref(inputstr,divider):    #result_list.extend("abc")=> ['a','b','c']    f=open(logfile,'w')    if debug==1:        f.write("expandref() get input: "+ inputstr + "\n")    position=inputstr.find(divider)    if position==-1:        return [inputstr]    str1=inputstr[:position]    str2=inputstr[position+1:]    # get ref prefix in str1    i=0    while str1[i].isalpha():        i=i+1        if i>=len(str1):            return [inputstr]    prefix1=str1[:i]    # get the start number of ref    numstart=str1[i:]    if numstart.isdigit():        numstart=int(numstart)    else:        return [inputstr]    if debug==1:        f.write("expandref(): prefix "+ prefix1 + "\n")        f.write("expandref(): numstart "+ str(numstart) + "\n")    # get the end number of ref    if str2.isdigit():        numend=int(str2)    elif str2[:i]==prefix1 and str2[i-1:].isdigit():        numend=int(str2)    else:        return [inputstr]    if debug==1:        f.write("expandref(): numend "+ str(numend) + "\n")    if numend<=numstart:        return [inputstr]    ref_l=[]    for i in range(numstart,numend+1):        ref_l.append(prefix1+str(i))    f.close()    return ref_ldef expandrefString(refstring,refdivider,expandseperator):    reflist=refstring.strip(refdivider).split(refdivider)    returnlist=[]    for ref in reflist:        if ref=='':            continue        returnlist.extend(expandref(ref,expandseperator))    return returnlist''' read value+refs.txt file,format like    "    10nFC12 C13    "    and print to be like    "    C1210nF    C1310nF    "    '''inputfile="./value+refs.txt"outputfile="./reflist.txt"fin=open(inputfile,'r')fout=open(outputfile,'w')seperator='\t'refSeperator=' 'for line in fin.readlines():    line=line.strip('\r\n').strip('\n').strip(' ').strip('\t')    length=len(line)    if(length==0):        continue    sepPositioin=line.find(seperator)    valueStr=line[:sepPositioin]    refList=expandrefString(line[sepPositioin+1:],' ','-')    print("reflist: "+str(refList))    for ref in refList:        fout.write(ref+seperator+valueStr+"\n")fin.close()fout.close()


0 0
原创粉丝点击