替换字符串在 python 3.2 中不起作用
我有一个模板文件:'template.txt'
如下所示:
class Core_Model_DbTable_{table_name} extends YouNet_Db_Table
{
const TYPE_PRINTED = 1;
const TYPE_DIGITAL = 2;
protected $_name = '{table_name}';
protected $_rowClass = 'Core_Model_{table_name:short}';
}
我使用 Python 3.2 读取该文件并尝试替换:
- {表名} =>优惠券
- {表名:短} =>优惠券
和这里是我的代码:
in_file = open("template.txt","r")
text = in_file.read()
in_file.close()
txt = text.replace("{table_name}","Coupons")
txt = text.replace("{table_name:short}","Coupon")
但输出只显示结果:
c:\Python32\python.exe builder.py
<?php
class Core_Model_DbTable_{table_name} extends YouNet_Db_Table
{
const TYPE_PRINTED = 1;
const TYPE_DIGITAL = 2;
protected $_name = '{table_name}';
protected $_rowClass = 'Core_Model_Coupon';
}
你能告诉我这里有什么问题吗?
I have a template file: 'template.txt'
like below:
class Core_Model_DbTable_{table_name} extends YouNet_Db_Table
{
const TYPE_PRINTED = 1;
const TYPE_DIGITAL = 2;
protected $_name = '{table_name}';
protected $_rowClass = 'Core_Model_{table_name:short}';
}
And I use Python 3.2 to read that file and try to replace:
- {table_name} => Coupons
- {table_name:short} => Coupon
and here my code:
in_file = open("template.txt","r")
text = in_file.read()
in_file.close()
txt = text.replace("{table_name}","Coupons")
txt = text.replace("{table_name:short}","Coupon")
But the output only shows the result:
c:\Python32\python.exe builder.py
<?php
class Core_Model_DbTable_{table_name} extends YouNet_Db_Table
{
const TYPE_PRINTED = 1;
const TYPE_DIGITAL = 2;
protected $_name = '{table_name}';
protected $_rowClass = 'Core_Model_Coupon';
}
Could you please tell me anything is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎拼错了变量的名称:“txt”与“text”。
You seem to have misspelled the name of your variable: "txt" vs "text".