如何读取txt文件 并且将内容创建成 xml

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

如何读取txt文件 并且将内容创建成 xml

Parsons Yang
大家好,小弟刚学python,非常喜欢
有一个问题想问一下大家

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

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

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


谢谢大家

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何读取txt文件 并且将内容创建成 xml

Anthony-106
Dive Into Python

On Jan 2, 2008 2:31 PM, Yang Yang <[hidden email]> wrote:
大家好,小弟刚学python,非常喜欢
有一个问题想问一下大家

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

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

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


谢谢大家

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese



--
---------------------------------------------------
www.douban.com/people/tutuqiang/
---------------------------------------------------
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何读取txt文件 并且将内容创建成 xml

Qutr
你想生成的XML文件格式是什么样的?
那些做Attribute,那些做Element?

 
在08-1-2,Tu Tu <[hidden email]> 写道:
Dive Into Python

On Jan 2, 2008 2:31 PM, Yang Yang <[hidden email]> wrote:
大家好,小弟刚学python,非常喜欢
有一个问题想问一下大家

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

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

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


谢谢大家

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank">http://python.cn/mailman/listinfo/python-chinese



--
---------------------------------------------------
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://www.douban.com/people/tutuqiang/" target="_blank">www.douban.com/people/tutuqiang/
---------------------------------------------------

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese



--
软件以程序员为本,程序员以技术为本!
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何读取txt文件 并且将内容创建成 xml

Qiangning Hong
In reply to this post by Parsons Yang
On Jan 2, 2008 2:31 PM, Yang Yang <[hidden email]> 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/
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何读取txt文件 并且将内容创建成 xml

Parsons Yang
In reply to this post by Qutr
类似这样的
 
<?xml version="1.0" encoding="GB2312"?>
<Table>
    <Record>
        <Title>1</Title>
        <Author>a1</Author>
        <Content>b1</Content>
        <Time>c1</Time>      
    </Record>
    <Record>
        <Title>2</Title>
        <Author>a2</Author>
        <Content>b2</Content>
        <Time>c2</Time>
    </Record>
    ...
</Table>

 
 
谢谢
 
在08-1-2,Qutr <[hidden email]> 写道:
你想生成的XML文件格式是什么样的?
那些做Attribute,那些做Element?

 
在08-1-2,Tu Tu <[hidden email]> 写道:
Dive Into Python

On Jan 2, 2008 2:31 PM, Yang Yang <[hidden email]> wrote:
大家好,小弟刚学python,非常喜欢
有一个问题想问一下大家

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

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

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


谢谢大家

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank">http://python.cn/mailman/listinfo/python-chinese



--
---------------------------------------------------
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://www.douban.com/people/tutuqiang/" target="_blank">www.douban.com/people/tutuqiang/
---------------------------------------------------

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank">http://python.cn/mailman/listinfo/python-chinese



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

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何读取txt文件 并且将内容创建成 xml

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

On Jan 2, 2008 3:16 PM, Yang Yang <[hidden email]> wrote:
类似这样的
 
<?xml version="1.0" encoding="GB2312"?>
<Table>
    <Record>
        <Title>1</Title>
        <Author>a1</Author>
        <Content>b1</Content>
        <Time>c1</Time>      
    </Record>
    <Record>
        <Title>2</Title>
        <Author>a2</Author>
        <Content>b2</Content>
        <Time>c2</Time>
    </Record>
    ...
</Table>

 
 
谢谢
 
在08-1-2,Qutr <[hidden email]> 写道:
你想生成的XML文件格式是什么样的?
那些做Attribute,那些做Element?

 
在08-1-2,Tu Tu <[hidden email]> 写道:
Dive Into Python

On Jan 2, 2008 2:31 PM, Yang Yang <[hidden email]> wrote:
大家好,小弟刚学python,非常喜欢
有一个问题想问一下大家

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

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

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


谢谢大家

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese



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

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese



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

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese



--
>: ~
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何读取txt文件 并且将内容创建成 xml

Wayne-39
In reply to this post by Parsons Yang
用ElementTree吧。详细用法见文档

在08-1-2,Yang Yang <[hidden email]> 写道:
类似这样的
 
<?xml version="1.0" encoding="GB2312"?>
<Table>
    <Record>
        <Title>1</Title>
        <Author>a1</Author>
        <Content>b1</Content>
        <Time>c1</Time>      
    </Record>
    <Record>
        <Title>2</Title>
        <Author>a2</Author>
        <Content>b2</Content>
        <Time>c2</Time>
    </Record>
    ...
</Table>

 
 
谢谢
 
在08-1-2,Qutr <[hidden email]> 写道:
你想生成的XML文件格式是什么样的?
那些做Attribute,那些做Element?

 
在08-1-2,Tu Tu <[hidden email]> 写道:
Dive Into Python

On Jan 2, 2008 2:31 PM, Yang Yang <[hidden email]> wrote:
大家好,小弟刚学python,非常喜欢
有一个问题想问一下大家

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

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

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


谢谢大家

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a href="http://python.cn/mailman/listinfo/python-chinese" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://python.cn/mailman/listinfo/python-chinese



--
---------------------------------------------------
<a href="http://www.douban.com/people/tutuqiang/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">www.douban.com/people/tutuqiang/
---------------------------------------------------

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a href="http://python.cn/mailman/listinfo/python-chinese" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://python.cn/mailman/listinfo/python-chinese



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

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: <a href="http://python.cn/mailman/listinfo/python-chinese" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese



--
wayne
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何读取txt文件 并且将内容创建成 xml

Qiangning Hong
In reply to this post by Wan Li
On Jan 2, 2008 3:39 PM, Question <[hidden email]> wrote:
> 有没有人研究过拼字符串和直接DOM操作哪个快,或者随attribute/node的数量变化有个什么曲线?

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


--
Qiangning Hong
http://www.douban.com/people/hongqn/
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese