Mako Template学习笔记
Mako Template介绍:Mako Template (odoo.com)
1. 简单小例子:
1 from mako.template import Template 2 3 my_template = Template("hello ${param}!") 4 print(my_template.render(param='world'))
运行结果为:
C:\Python27\python.exe C:/Sandbox/Python_practice/study/study_mako/study_template/1.py
hello world!
Process finished with exit code 0
注意其中render()函数将"world"传给param,有点类似format()函数:

2. 使用文件路径做为Template()函数的参数:
在python脚本同级路径下新建一个名为“test1.txt”的文件,内容为:
1 123 2 456
然后运行如下脚本:
1 from mako.template import Template 2 3 my_template = Template(filename="test1.txt") 4 print(my_template.render())
得到结果:
C:\Python27\python.exe C:/Sandbox/Python_practice/study/study_mako/study_template/2.py 123 456 Process finished with exit code 0
3. 根据上面的结果,可知render()函数会将把模板文件(传给Template()的参数)被处理后的结果以字符串形式返回:

因此我们就可以把render()函数的返回值写入另一个文件:
1 from mako.template import Template 2 3 my_template = Template(filename="test1.txt") 4 with open('test2.txt', 'wb') as f: 5 f.write(my_template.render()) 6 7 with open('test2.txt', 'rb') as f: 8 for line in f.readlines(): 9 print(line.strip())
运行结果为:
C:\Python27\python.exe C:/Sandbox/Python_practice/study/study_mako/study_template/3.py 123 456 Process finished with exit code 0
4. 有了以上测试结果,我们便可以做一些真正想做的操作,比如将input文件中的有效信息提取出来,然后生成到一个c文件的数组中去:
例如我有一个文件“test3.txt”内容如下,它里面记录了两个学生的姓名,性别和年龄信息:
1 "Michael" MALE 16 2 "Jane" FEMALE 15
我的目的是想把这个信息填入数组,得到一个内容如下的c文件:
1 #define STUDENT_NUMBER (2u) 2 3 const STUDENT_Type studentInfo[STUDENT_NUMBER] = 4 { 5 {"Michael", MALE, 16u}, 6 {"Jane", FEMALE, 15u}, 7 }
为了达成这个目的,设计如下思路:
- 将input文件(“test3.txt”)的信息塞到一个名为“studentData”的字典类型变量中
- 构建模板文件,在合适的地方使用替换表达式("${data}")便于将“data”的内容填入
- 使用render()函数将“studentData”传给模板文件需要的“data”
- 将最终结果输入到目标文件中
执行第一步:
1 studentNumber = 0 2 studentInfo = [] 3 4 inputFile = "test3.txt" 5 with open(inputFile, 'rb') as f: 6 for line in f.readlines(): 7 studentNumber += 1 8 name, sex, age = line.strip().split(' ') 9 student = {'name': name, 'sex': sex, 'age': age} 10 studentInfo.append(student) 11 12 studentData = {'studentNumber': studentNumber, 'student': studentInfo}
执行第二步,构建模板文件“student.template.c”如下:
1 #define STUDENT_NUMBER (${data['studentNumber']}u) 2 3 const STUDENT_Type studentInfo[STUDENT_NUMBER] = 4 { 5 % for i in range(data['studentNumber']): 6 {${data['student'][i]['name']}, ${data['student'][i]['sex']}, ${data['student'][i]['age']}u}, 7 % endfor 8 }
执行最后两步,生成目标文件“student.c”:
1 my_template = Template(filename="student.template.c") 2 with open('student.c', 'wb') as f: 3 f.write(my_template.render(data=studentData))
整个脚本内容如下:
1 from mako.template import Template 2 3 studentNumber = 0 4 studentInfo = [] 5 6 inputFile = "test3.txt" 7 with open(inputFile, 'rb') as f: 8 for line in f.readlines(): 9 studentNumber += 1 10 name, sex, age = line.strip().split(' ') 11 student = {'name': name, 'sex': sex, 'age': age} 12 studentInfo.append(student) 13 14 studentData = {'studentNumber': studentNumber, 'student': studentInfo} 15 16 my_template = Template(filename="student.template.c") 17 with open('student.c', 'wb') as f: 18 f.write(my_template.render(data=studentData))
执行后生成“student.c”文件内容如下,与目标一致:
1 #define STUDENT_NUMBER (2u) 2 3 const STUDENT_Type studentInfo[STUDENT_NUMBER] = 4 { 5 {"Michael", MALE, 16u}, 6 {"Jane", FEMALE, 15u}, 7 }
浙公网安备 33010602011771号