Python解析csv檔案並將結果寫入bat檔案
阿新 • • 發佈:2018-12-15
第二次感覺有很大進步,對列表使用慢慢熟悉,繼續加油。
#!/usr/bin/python # -*- coding: UTF-8 -*- import os import csv import codecs import re import sys from copy import deepcopy print 'Run:',sys.argv[0] csvTitle = [‘A’, ‘B’, ‘C’, ‘templet’, ] 1 print csvTitle teamName = [‘A’, ‘B’, ‘C’, ‘route_templet’, 'S' ] route_a =[] route_b =[] route_c =[] #route_templet =[] dict_route = {teamName[0]:route_a, teamName[1]:route_b, teamName[2]:route_c, #teamName[6]:route_templet, } #巨集 csvTitleLen = len(csvTitle) teamNameLen = len(teamName) inpath = ‘H:C\表.csv’ source = ‘xcopy /y /S /I H:\’ target = ’ %WORKSPACE%\code\’ bat_fileName = “code_xcopy.bat” bat_routeName = “route.txt” #檔案生成路徑 outPath =[] for outDir in teamName: outPath.append(“G:/workspace/CCM/” + outDir + “/bat”) #深拷貝 dict_col_route = deepcopy(dict_route)#從csv檔案獲取的的程式碼路徑資料 dict_bat_route = deepcopy(dict_route)#輸出到bat檔案的程式碼路徑資料 dict_out_route = deepcopy(dict_route)#輸出到txt檔案的程式碼路徑資料 def printDict(dict_route,i): for i in dict_route[teamName[i]]: print i def getData(path): uipath = unicode(path,"utf8") data = open(uipath,'r') return csv.reader(data) def csv_prase(path,dict_route): dataline = getData(path) for row in dataline: for i in range (0,csvTitleLen): if row[1] == csvTitle[i]: dict_route[teamName[i]].append(row[5]) def change_route(route, bat_route, out_route): route = route.replace('/','\\') route = route.rstrip('\\') if route.find(‘_pm’) == 0: route = route.replace(‘opm’,‘R\co\olm’,1) bat_route.append(source + route + target + route) elif route.find(‘00’) == 0: route = route.replace(‘00’,‘NG\c0’,1) bat_route.append(source + route + target + route) elif route.find(‘ub’) == 0: route.replace(‘ub’,‘oub’,1) else : bat_route.append(source + route + target + route) out_route.append(route) def route_prase(path,col_route,bat_route,out_route): csv_prase(path,col_route) for i in range (0,teamNameLen): print '********************************************************' print teamName[i] print '********************************************************' for route in col_route[teamName[i]]: change_route(route,bat_route[teamName[i]], out_route[teamName[i]]) printDict(out_route,i) def del_route_bat(targetPath, fileName): pwd = os.getcwd() os.chdir(targetPath) if os.path.exists(fileName): os.remove(fileName) os.chdir(pwd) def add_route_bat(str_r,targetPath, bat_route, fileName): pwd = os.getcwd() os.chdir(targetPath) f = file(fileName, "a+") f = open(fileName, "w") f.write(str_r) f.write('\n') f.write('\n') for line in bat_route: f.write(line) f.write('\n') f.close os.chdir(pwd) def dat_del_opr(path,fileName): for i in path: del_route_bat(i, fileName) def dat_add_opr(str_r,path,fileName,dict_route): dat_del_opr(path,fileName) for i in range (0,teamNameLen): add_route_bat(str_r,path[i],dict_route[teamName[i]], fileName) if (__name__ == "__main__"): route_prase(inpath,dict_col_route,dict_bat_route,dict_out_route) dat_add_opr('g:',outPath,bat_fileName,dict_bat_route) dat_add_opr(' ',outPath,bat_routeName,dict_out_route)