1. 程式人生 > >Python解析csv檔案並生成解析的bat檔案

Python解析csv檔案並生成解析的bat檔案

import os
import csv
import codecs
import re
import sys
from copy import deepcopy

print ‘Run:’,sys.argv[0]
##注意對應,需要增刪只需增刪csvTitle、teamName、dict_route內容即可
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(‘olt_pm’) == 0:
		route = route.replace(‘olt_pm’,‘ZXROSNG\code\olt_pm’,1)
		bat_route.append(source + route + target + route)
	elif route.find(‘c600’) == 0:
		route = route.replace(‘c600’,‘ZXROSNG\c600’,1)
		bat_route.append(source + route + target + route)
	elif route.find(‘olt_pub’) == 0:
		route.replace(‘olt_pub’,‘olt_pub’,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)