-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv_to_csharp.py
86 lines (73 loc) · 2.9 KB
/
csv_to_csharp.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
csv转换成C#代码工具
@author: ChenXing
@email: onechenxing@163.com
@date: 2015/1/17
"""
import glob
def buildCode(filePath):
#类模版
classTemplate = """
using System;
namespace Data
{
/// <summary>
/// 静态表数据类
/// </summary>
[Serializable]
public class $className
{
$paramFied
public $className Clone()
{
return ($className) this.MemberwiseClone();
}
}
}
"""
#属性模版
paramTemplate = """
/// <summary>
/// $paramNote
/// </summary>
public $paramType $paramName;"""
className = filePath.split("\\")[-1].split(".")[0] + "Info"
code = "";
try:
with open(filePath, encoding = "utf-8") as dataFile:
notes = dataFile.readline().strip('\n').split(",")
params = dataFile.readline().strip('\n').split(",")
for i,paramName in enumerate(params):
try:
paramNote = notes[i]
#默认类型string
paramType = "string"
#查找和判断类型
findIndex = 0;
findIndex = paramNote.rfind("[int]")
if(findIndex != -1):
paramType = "int"
paramNote = paramNote[0:findIndex]
findIndex = paramNote.rfind("[float]")
if(findIndex != -1):
paramType = "float"
paramNote = paramNote[0:findIndex]
#生成属性
code += paramTemplate.replace("$paramNote", paramNote).replace("$paramName", paramName).replace("$paramType",paramType)
except ValueError:
pass
#生成类
code = classTemplate.replace("$className", className).replace("$paramFied", code)
except IOError as err:
print("The data file is missing:" + str(err))
try:
with open("csv\\" + className + ".cs", "w", encoding = "utf-8") as outFile:
print(code, end="", file = outFile)
except IOError:
print("write file error")
#导出csv目录下所有文件
csvFiles = glob.glob("csv\\*.csv")
for eachFile in csvFiles:
buildCode(eachFile)
print("csv_to_csharp ok.")
#input()