Re: [python-chinese] 如何读取txt文件 并且将内容创建成 xml

15 views
Skip to first unread message

Qutr

unread,
Jan 2, 2008, 1:56:36 AM1/2/08
to python-...@lists.python.cn
你想生成的XML文件格式是什么样的?
那些做Attribute,那些做Element?

 
在08-1-2,Tu Tu <tu.tu...@gmail.com> 写道:
Dive Into Python

On Jan 2, 2008 2:31 PM, Yang Yang <scrip...@gmail.com> wrote:
大家好,小弟刚学python,非常喜欢
有一个问题想问一下大家

我从数据库中取出4个字段,用$分割每个字段,生成了一个txt文件
现在,我想要用python,读取这个文件,并且将里面的内容生成一个xml文件

文本文档例子如下:
1$a1$b1$c1
2$a2$b2$c2

请教一下如何读取,然后生成xml文件
最好能够给一个简单的例子


谢谢大家

_______________________________________________
python-chinese
Post: send python-...@lists.python.cn
Subscribe: send subscribe to python-chin...@lists.python.cn
Unsubscribe: send unsubscribe to  python-chin...@lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese



--
---------------------------------------------------
www.douban.com/people/tutuqiang/
---------------------------------------------------

_______________________________________________
python-chinese
Post: send python-...@lists.python.cn
Subscribe: send subscribe to python-chin...@lists.python.cn
Unsubscribe: send unsubscribe to  python-chin...@lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese



--
软件以程序员为本,程序员以技术为本!

Qiangning Hong

unread,
Jan 2, 2008, 2:14:41 AM1/2/08
to python-...@lists.python.cn
On Jan 2, 2008 2:31 PM, Yang Yang <scrip...@gmail.com> wrote:
> 大家好,小弟刚学python,非常喜欢
> 有一个问题想问一下大家
>
> 我从数据库中取出4个字段,用$分割每个字段,生成了一个txt文件
> 现在,我想要用python,读取这个文件,并且将里面的内容生成一个xml文件
>
> 文本文档例子如下:
> 1$a1$b1$c1
> 2$a2$b2$c2
>
> 请教一下如何读取,然后生成xml文件
> 最好能够给一个简单的例子

txt = open('data.txt').read()
xml = open('kidding.xml', 'w')
print >>xml, "<?xml version="1.0"?>"
print >>xml, "<kidding><[CDATA["
print >>xml, txt
print >>xml, "]]></kidding>"
xml.close()

--
Qiangning Hong
http://www.douban.com/people/hongqn/

Qiangning Hong

unread,
Jan 2, 2008, 3:12:57 AM1/2/08
to python-...@lists.python.cn
On Jan 2, 2008 3:39 PM, Question <wanl...@gmail.com> wrote:
> 有没有人研究过拼字符串和直接DOM操作哪个快,或者随attribute/node的数量变化有个什么曲线?

显然是拼字符串快,操作DOM完了输出归根结底还是要拼字符串的

徐梓鼎

unread,
Jan 2, 2008, 3:59:12 AM1/2/08
to pyth...@googlegroups.com

在2008-01-02来自hon...@gmail.com的邮件[[CPyUG:37609] Re: [python-chinese]
如何读取txt文件 并且将内容创建成 xml]中写到:

> > 有没有人研究过拼字符串和直接DOM操作哪个快,或者随attribute/node的数量变化有个什么曲线?
>
> 显然是拼字符串快,操作DOM完了输出归根结底还是要拼字符串的

直接拼字符串倒是快,不过很容易搞得你的 xml 只有你自己搞得懂,其他的解析
器全都不行。比如你的某个值里面有个<


--
DEBUG是件快乐的事情 <xuzi...@gmail.com>


Reply all
Reply to author
Forward
0 new messages