2018-10-26

python 批量重命名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
picResource = raw_input(r"input the resource dir:")
print picResource
prefix = raw_input(r"input the prefix : ")
zeroPos = raw_input(r"input the fix 0 pos,default is 0:")
<!--more-->
if zeroPos == '':
zeroPos = '0'
filelist = os.listdir(picResource)
index = 0
for file in filelist:
afterFix = file.split(".")[1]
index = index + 1
os.rename(os.path.join(picResource,file),os.path.join(picResource,prefix + str(index - 1).zfill(int(zeroPos) + 1) + "." + afterFix))
print(r"---rename successful -->")
raw_input(r"press enter to close this window")

读取CSV配置表

表格:
| ID |Name|Age|
|——|——|——|
|001|player1|10|
|002|player2|23|

读取代码

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
loadCSV(str:string)
{
let self = this;
cc.loader.loadRes(str, function (err, csvasset) {
if(err)
{
console.log("load config error");
}
else
{
let objArr = self.analysisCSV(csvasset.text);
self.getDataFromObjArr(str,objArr);
}
});
}
analysisCSV(info:string)
{
this.showLog(info);
let lines = info.split('\r\n');
let properties = lines[0].split(',');
let types = lines[1].split(',');
let objArr:Object[] = [];
for(let i:number = 2;i<lines.length;++i)
{
let obj:Object = new Object;
let curProperties = lines[i].split(',');
for(let j:number = 0;j<properties.length;++j)
{
//转换类型
if(types[j] == 'I')
{
obj[properties[j]] = parseInt(curProperties[j]);
}
else
{
obj[properties[j]] = curProperties[j];
}
}
objArr.push(obj);
}
console.log(objArr);
}