Python對(duì)csv文件的處理
公司策劃那邊是不是會(huì)有從相關(guān)csv文件提取相關(guān)屬性所對(duì)應(yīng)的所有值的需求。 為了避免手動(dòng)一個(gè)文件一個(gè)文件的查找&復(fù)制中姜,寫了一個(gè)腳本進(jìn)行對(duì)應(yīng)查找和提取嘉竟。
比如前兩天有個(gè)需求是:
給出一組questid列表,在每個(gè)questid(例如99070333)后面添加一個(gè)
0
(變成了990703330
),然后根據(jù)這個(gè)questid + "0"
(我們定義為stageid)找出應(yīng)目錄下的csv文件,這些csv文件的文件名格式形式為:prefix_stageid
.surfix
毁菱。在項(xiàng)目中prefix
為mapEventMonster,surfix
為csv米死。因此以questid = 99070333為例,對(duì)應(yīng)的csv文件名為:
mapEventMonster_990703330.csv
這些csv文件格式如下(屬性和值之間由逗號(hào)分隔,這里為了表示的方式來顯示效果):
| stageID | roomID | rate | enemyGroupID| actionFlg|messageNo_0|messageNo_1|messageNo_2|messageNo_3|messageNo_4|worldID|areaID|dungeonID|
|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
|990701690|2|0|90080169|1|-1|-1|-1|-1|-1|9|907|1|
這里我們提取enemyGroupID屬性所對(duì)應(yīng)的所有值贮庞。
這里還有一個(gè)問題是:所有csv文件按照相關(guān)規(guī)則保存在對(duì)應(yīng)目錄中峦筒,例如:
...
dungeon/quest_type_3/area_907/mapEventMonster_990701690.csv
dungeon/quest_type_3/area_999/mapEventMonster_999999990.csv
...
...
所以我們需要找深入到目錄(及其子目錄中;這里一級(jí)目錄為dungeon
)找到對(duì)應(yīng)csv文件窗慎。同時(shí)最好建立一個(gè)key-value
的字典結(jié)構(gòu)用來保存所有的查找結(jié)果物喷。這里key
為stageId(即questId + "0"); value
為對(duì)應(yīng)csv文件的全路徑。
代碼如下:
#!/usr/bin/python
# coding:utf8
import os
import csv
# 遞歸目錄及其子目錄下的文件
def dirlist(path, allfile):
filelist = os.listdir(path)
for filename in filelist:
filepath = os.path.join(path, filename)
if os.path.isdir(filepath):
dirlist(filepath, allfile)
else:
allfile.append(filepath)
return filepath
# 從文件的全路徑中截取文件名(一般處于全路徑的最后一個(gè)'/'的后面)
def cutFileName(fullpath):
return fullpath.split('/')[-1]
# 打印list所有元素
def printList(myList):
for item in myList:
print item
# 判斷文件名是否滿足指定后綴
def isSurfix(path, surfix):
filename = cutFileName(path)
return path.split('.')[-1] == surfix
# 判斷文件名是否滿足指定前綴
def isPrefix(path, prefix):
filename = cutFileName(path)
return filename.find(prefix) == 0
# 建立key-value的字典結(jié)構(gòu)保存搜索結(jié)果
# key: stageId
# value: 對(duì)應(yīng)csv的全路徑
def setDict(allfile, prefix, surfix):
myDict = dict()
for item in allfile:
if isSurfix(item, surfix) and isPrefix(item, prefix):
filename = cutFileName(item)
beg = filename.find(prefix)
end = filename.find(surfix)
stageId = filename[beg + len(prefix): end-1]
myDict[stageId] = item
return myDict
# 獲得對(duì)應(yīng)csv文件的某一列的所有值
def getCsvValue(filepath, colnum, ret):
# print filepath
reader = csv.reader(file(filepath, 'rb'))
for line in reader:
if reader.line_num == 1:
# 跳過第一行屬性名
continue
ret.append(line[colnum])
# 遞歸創(chuàng)建目錄
def createDirs(path,outputpath):
idx = path.find('dungeon')
subpath = path[idx : ]
#print subpath
outputfullpath = os.path.join(outputpath, subpath)
idx = outputfullpath.rfind("/")
outputfullpath = outputfullpath[0:idx]
#print outputfullpath
if not os.path.exists(outputfullpath):
os.makedirs(outputfullpath)
# 拷貝對(duì)應(yīng)文件
def copyFile(srcfile, outputpath):
idx = srcfile.find('dungeon')
subpath = srcfile[idx : len(srcfile)]
outputfullpath = os.path.join(outputpath, subpath)
# 拷貝文件
shutil.copyfile(srcfile, outputfullpath)
# main函數(shù)
if __name__ == '__main__':
#以下各變量需要根據(jù)實(shí)際情況補(bǔ)全遮斥,
# 否則無法運(yùn)行
path = # "需要掃描的目錄路徑"
prefix = # "例如:mapEventMonster_"
surfix = # "例如:csv"
inputfile = # 例如:open('輸入文件名(含路徑)', 'rb')
allfile = []
dirlist(path, allfile)
myDict = setDict(allfile, prefix, surfix)
ret = list()
for line in inputfile:
key = line.strip("\n") + "0"
getCsvValue(myDict[key], 3, ret)
ret = list(set(ret)) # 去重
ret.sort() # 排序
printList(ret) # 打印
inputfile.close()