2005年06月10日 星期五 11:25
---------- Forwarded message ---------- From: limodou <limodou at gmail.com> Date: 2005-6-10 上午11:25 Subject: Re: 再问一下?? To: 程启盛 <cqs1015 at 163.com> 就用代码写就行啊。如: f = file('a.html', 'wb') txt=[] #保存html代码片段 txt.append('''%s ''' % title) #生成html体 ... txt.append(snip) ... #写入html尾 txt.append(''' ''') f.write(''.join(txt)) f.close() 这样不就行了嘛。 复杂点的还可以使用模板之类的技术。 在 05-6-10,程启盛<cqs1015 at 163.com> 写道: > limodou: > 你好! > 如何直接生成html文件?我想做的就是把所有收藏或书签保存到HTML文件. > > CQS > 6.10 > > > > 因为你要保存为html,可以直接生成html文件呀。而cPickle是一种内部格式,生成出来的文件也不是html的格式,不知道你要它做什么。 > > > > > > > > > > > > > 已有1亿用户选择了网易邮箱,你呢? > 网易163免费邮已全面升级至2000兆超大空间,支持收发30兆超大附件! > 注册登录网易邮箱,每周都有惊喜,50元现金券完全白送,点击查看活动详情! -- I like python! My Donews Blog: http://www.donews.net/limodou New Google Maillist: http://groups-beta.google.com/group/python-cn -- I like python! My Donews Blog: http://www.donews.net/limodou New Google Maillist: http://groups-beta.google.com/group/python-cn
2005年06月10日 星期五 12:50
其实,直接利用 Python 自个儿的字串操作就可以进行所谓模板处理了!
== 简要模板 ==
* 高级 字串格式化替换
* locals() 将当前运行空间中的所有对象以字典方式转换出来
可以使用 %(varName)s 的模式进行字串替换
* 这样一来就形成了一个天然的模板系统
* 比如说模板文件 ask-item.php
{{{
%(ppqitem)s
}}}
* 使用时,就是读入,然后替换文件,组织,输出为其它文件
* 比如说
{{{#!python
ff = open("paper.php","w")
exp = ""
template = open(ask-item.php,"r").read()
for item in question:
ppqvar = item[1][0]
ppqitem = item[1][1]
exp +=template%locals()
ff.write(exp)
}}}
* 其中 '''question''' 是一个结构化数据对象
* 在循环中,'''locals()''' 会自动将当前环境对象中与模板字串中替换字串声明匹配的对象值替换入!
在 05-6-10,limodou<limodou at gmail.com> 写道:
> ---------- Forwarded message ----------
> From: limodou <limodou at gmail.com>
> Date: 2005-6-10 上午11:25
> Subject: Re: 再问一下??
> To: 程启盛 <cqs1015 at 163.com>
>
> 就用代码写就行啊。如:
> f = file('a.html', 'wb')
> txt=[] #保存html代码片段
> txt.append('''
>
> %s
>
>
> ''' % title)
> #生成html体
> ...
> txt.append(snip)
> ...
> #写入html尾
> txt.append('''
> ''')
> f.write(''.join(txt))
> f.close()
>
> 这样不就行了嘛。
>
> 复杂点的还可以使用模板之类的技术。
>
> 在 05-6-10,程启盛<cqs1015 at 163.com> 写道:
> > limodou:
> > 你好!
> > 如何直接生成html文件?我想做的就是把所有收藏或书签保存到HTML文件.
> >
> > CQS
> > 6.10
> >
> > >
> > 因为你要保存为html,可以直接生成html文件呀。而cPickle是一种内部格式,生成出来的文件也不是html的格式,不知道你要它做什么。
> > >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > 已有1亿用户选择了网易邮箱,你呢?
> > 网易163免费邮已全面升级至2000兆超大空间,支持收发30兆超大附件!
> > 注册登录网易邮箱,每周都有惊喜,50元现金券完全白送,点击查看活动详情!
>
> --
> I like python!
> My Donews Blog: http://www.donews.net/limodou
> New Google Maillist: http://groups-beta.google.com/group/python-cn
>
>
> --
> I like python!
> My Donews Blog: http://www.donews.net/limodou
> New Google Maillist: http://groups-beta.google.com/group/python-cn
>
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
--
[Time is unimportant, only life important!]
Zeuux © 2025
京ICP备05028076号