Python论坛  - 讨论区

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

2008年01月02日 星期三 14:31

Yang Yang scriptwolf在gmail.com
星期三 一月 2 14:31:12 HKT 2008

´ó¼ÒºÃ£¬Ð¡µÜ¸Õѧpython£¬·Ç³£Ï²»¶
ÓÐÒ»¸öÎÊÌâÏëÎÊһϴó¼Ò

ÎÒ´ÓÊý¾Ý¿âÖÐÈ¡³ö4¸ö×ֶΣ¬ÓÃ$·Ö¸îÿ¸ö×ֶΣ¬Éú³ÉÁËÒ»¸ötxtÎļþ
ÏÖÔÚ£¬ÎÒÏëÒªÓÃpython£¬¶ÁÈ¡Õâ¸öÎļþ£¬²¢ÇÒ½«ÀïÃæµÄÄÚÈÝÉú³ÉÒ»¸öxmlÎļþ

Îı¾ÎĵµÀý×ÓÈçÏ£º
1$a1$b1$c1
2$a2$b2$c2

Çë½ÌÒ»ÏÂÈçºÎ¶ÁÈ¡£¬È»ºóÉú³ÉxmlÎļþ
×îºÃÄܹ»¸øÒ»¸ö¼òµ¥µÄÀý×Ó


лл´ó¼Ò
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080102/36081afb/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月02日 星期三 14:34

Tu Tu tu.tuqiang在gmail.com
星期三 一月 2 14:34:19 HKT 2008

Dive Into Python

On Jan 2, 2008 2:31 PM, Yang Yang <scriptwolf在gmail.com> wrote:

> ´ó¼ÒºÃ£¬Ð¡µÜ¸Õѧpython£¬·Ç³£Ï²»¶
> ÓÐÒ»¸öÎÊÌâÏëÎÊһϴó¼Ò
>
> ÎÒ´ÓÊý¾Ý¿âÖÐÈ¡³ö4¸ö×ֶΣ¬ÓÃ$·Ö¸îÿ¸ö×ֶΣ¬Éú³ÉÁËÒ»¸ötxtÎļþ
> ÏÖÔÚ£¬ÎÒÏëÒªÓÃpython£¬¶ÁÈ¡Õâ¸öÎļþ£¬²¢ÇÒ½«ÀïÃæµÄÄÚÈÝÉú³ÉÒ»¸öxmlÎļþ
>
> Îı¾ÎĵµÀý×ÓÈçÏ£º
> 1$a1$b1$c1
> 2$a2$b2$c2
>
> Çë½ÌÒ»ÏÂÈçºÎ¶ÁÈ¡£¬È»ºóÉú³ÉxmlÎļþ
> ×îºÃÄܹ»¸øÒ»¸ö¼òµ¥µÄÀý×Ó
>
>
> лл´ó¼Ò
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
---------------------------------------------------
www.douban.com/people/tutuqiang/
---------------------------------------------------
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080102/187cdf52/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月02日 星期三 14:56

Qutr qutianrang在gmail.com
星期三 一月 2 14:56:36 HKT 2008

ÄãÏëÉú³ÉµÄXMLÎļþ¸ñʽÊÇʲôÑùµÄ£¿
ÄÇЩ×öAttribute£¬ÄÇЩ×öElement£¿


ÔÚ08-1-2£¬Tu Tu <tu.tuqiang在gmail.com> дµÀ£º
>
> Dive Into Python
>
>  On Jan 2, 2008 2:31 PM, Yang Yang <scriptwolf在gmail.com> wrote:
>
> > ´ó¼ÒºÃ£¬Ð¡µÜ¸Õѧpython£¬·Ç³£Ï²»¶
> > ÓÐÒ»¸öÎÊÌâÏëÎÊһϴó¼Ò
> >
> > ÎÒ´ÓÊý¾Ý¿âÖÐÈ¡³ö4¸ö×ֶΣ¬ÓÃ$·Ö¸îÿ¸ö×ֶΣ¬Éú³ÉÁËÒ»¸ötxtÎļþ
> > ÏÖÔÚ£¬ÎÒÏëÒªÓÃpython£¬¶ÁÈ¡Õâ¸öÎļþ£¬²¢ÇÒ½«ÀïÃæµÄÄÚÈÝÉú³ÉÒ»¸öxmlÎļþ
> >
> > Îı¾ÎĵµÀý×ÓÈçÏ£º
> > 1$a1$b1$c1
> > 2$a2$b2$c2
> >
> > Çë½ÌÒ»ÏÂÈçºÎ¶ÁÈ¡£¬È»ºóÉú³ÉxmlÎļþ
> > ×îºÃÄܹ»¸øÒ»¸ö¼òµ¥µÄÀý×Ó
> >
> >
> > лл´ó¼Ò
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> ---------------------------------------------------
> www.douban.com/people/tutuqiang/
> ---------------------------------------------------
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
Èí¼þÒÔ³ÌÐòԱΪ±¾£¬³ÌÐòÔ±ÒÔ¼¼ÊõΪ±¾£¡
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080102/14595bff/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月02日 星期三 15:14

Qiangning Hong hongqn在gmail.com
星期三 一月 2 15:14:41 HKT 2008

On Jan 2, 2008 2:31 PM, Yang Yang <scriptwolf在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, ""
print >>xml, "<[CDATA["
print >>xml, txt
print >>xml, "]]>"
xml.close()

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月02日 星期三 15:16

Yang Yang scriptwolf在gmail.com
星期三 一月 2 15:16:53 HKT 2008

ÀàËÆÕâÑùµÄ



        1
        a1
        b1
        
    
        2
        a2
        b2
        
    
    ...
лл ÔÚ08-1-2£¬Qutr <qutianrang在gmail.com> дµÀ£º > > ÄãÏëÉú³ÉµÄXMLÎļþ¸ñʽÊÇʲôÑùµÄ£¿ > ÄÇЩ×öAttribute£¬ÄÇЩ×öElement£¿ > > > ÔÚ08-1-2£¬Tu Tu <tu.tuqiang在gmail.com> дµÀ£º > > > > Dive Into Python > > > > On Jan 2, 2008 2:31 PM, Yang Yang <scriptwolf在gmail.com> wrote: > > > > > ´ó¼ÒºÃ£¬Ð¡µÜ¸Õѧpython£¬·Ç³£Ï²»¶ > > > ÓÐÒ»¸öÎÊÌâÏëÎÊһϴó¼Ò > > > > > > ÎÒ´ÓÊý¾Ý¿âÖÐÈ¡³ö4¸ö×ֶΣ¬ÓÃ$·Ö¸îÿ¸ö×ֶΣ¬Éú³ÉÁËÒ»¸ötxtÎļþ > > > ÏÖÔÚ£¬ÎÒÏëÒªÓÃpython£¬¶ÁÈ¡Õâ¸öÎļþ£¬²¢ÇÒ½«ÀïÃæµÄÄÚÈÝÉú³ÉÒ»¸öxmlÎļþ > > > > > > Îı¾ÎĵµÀý×ÓÈçÏ£º > > > 1$a1$b1$c1 > > > 2$a2$b2$c2 > > > > > > Çë½ÌÒ»ÏÂÈçºÎ¶ÁÈ¡£¬È»ºóÉú³ÉxmlÎļþ > > > ×îºÃÄܹ»¸øÒ»¸ö¼òµ¥µÄÀý×Ó > > > > > > > > > лл´ó¼Ò > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request在lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > -- > > --------------------------------------------------- > > www.douban.com/people/tutuqiang/ > > --------------------------------------------------- > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > -- > Èí¼þÒÔ³ÌÐòԱΪ±¾£¬³ÌÐòÔ±ÒÔ¼¼ÊõΪ±¾£¡ > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20080102/7a8ecee6/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月02日 星期三 15:39

Question wanliyou在gmail.com
星期三 一月 2 15:39:38 HKT 2008

有没有人研究过拼字符串和直接DOM操作哪个快,或者随attribute/node的数量变化有个什么曲线?

On Jan 2, 2008 3:16 PM, Yang Yang <scriptwolf at gmail.com> wrote:

> 类似这样的
>
> 
> 
> > 1 > a1 > b1 > > > > 2 > a2 > b2 > > > ... > > > > > 谢谢 > > 在08-1-2,Qutr <qutianrang at gmail.com> 写道: > > > 你想生成的XML文件格式是什么样的? > > 那些做Attribute,那些做Element? > > > > > > 在08-1-2,Tu Tu <tu.tuqiang at gmail.com> 写道: > > > > > > Dive Into Python > > > > > > On Jan 2, 2008 2:31 PM, Yang Yang <scriptwolf at gmail.com> wrote: > > > > > > > 大家好,小弟刚学python,非常喜欢 > > > > 有一个问题想问一下大家 > > > > > > > > 我从数据库中取出4个字段,用$分割每个字段,生成了一个txt文件 > > > > 现在,我想要用python,读取这个文件,并且将里面的内容生成一个xml文件 > > > > > > > > 文本文档例子如下: > > > > 1$a1$b1$c1 > > > > 2$a2$b2$c2 > > > > > > > > 请教一下如何读取,然后生成xml文件 > > > > 最好能够给一个简单的例子 > > > > > > > > > > > > 谢谢大家 > > > > > > > > _______________________________________________ > > > > python-chinese > > > > Post: send python-chinese at lists.python.cn > > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > > Unsubscribe: send unsubscribe to > > > > python-chinese-request at lists.python.cn > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > -- > > > --------------------------------------------------- > > > www.douban.com/people/tutuqiang/ > > > --------------------------------------------------- > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request at lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > -- > > 软件以程序员为本,程序员以技术为本! > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- >: ~ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20080102/e5321ae8/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月02日 星期三 15:46

Wayne moonbingbing在gmail.com
星期三 一月 2 15:46:43 HKT 2008

用ElementTree吧。详细用法见文档

在08-1-2,Yang Yang <scriptwolf at gmail.com> 写道:
>
> 类似这样的
>
> 
> 
> > 1 > a1 > b1 > > > > 2 > a2 > b2 > > > ... > > > > > 谢谢 > > 在08-1-2,Qutr <qutianrang at gmail.com> 写道: > > > > 你想生成的XML文件格式是什么样的? > > 那些做Attribute,那些做Element? > > > > > > 在08-1-2,Tu Tu <tu.tuqiang at gmail.com> 写道: > > > > > > Dive Into Python > > > > > > On Jan 2, 2008 2:31 PM, Yang Yang <scriptwolf at gmail.com> wrote: > > > > > > > 大家好,小弟刚学python,非常喜欢 > > > > 有一个问题想问一下大家 > > > > > > > > 我从数据库中取出4个字段,用$分割每个字段,生成了一个txt文件 > > > > 现在,我想要用python,读取这个文件,并且将里面的内容生成一个xml文件 > > > > > > > > 文本文档例子如下: > > > > 1$a1$b1$c1 > > > > 2$a2$b2$c2 > > > > > > > > 请教一下如何读取,然后生成xml文件 > > > > 最好能够给一个简单的例子 > > > > > > > > > > > > 谢谢大家 > > > > > > > > _______________________________________________ > > > > python-chinese > > > > Post: send python-chinese at lists.python.cn > > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > > Unsubscribe: send unsubscribe to > > > > python-chinese-request at lists.python.cn > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > -- > > > --------------------------------------------------- > > > www.douban.com/people/tutuqiang/ > > > --------------------------------------------------- > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request at lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > -- > > 软件以程序员为本,程序员以技术为本! > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20080102/0461e790/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月02日 星期三 16:12

Qiangning Hong hongqn在gmail.com
星期三 一月 2 16:12:57 HKT 2008

On Jan 2, 2008 3:39 PM, Question <wanliyou在gmail.com> wrote:
> 有没有人研究过拼字符串和直接DOM操作哪个快,或者随attribute/node的数量变化有个什么曲线?

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


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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号