【python】lxml-The E-factory

来自:http://lxml.de/tutorial.html

lxml中的E-factory可以用个简单快速的生成XML和HTML

>>> from lxml.builder import E

>>> def CLASS(*args): # class is a reserved word in Python
...     return {"class":‘ ‘.join(args)}

>>> html = page = (
...   E.html(       # create an Element called "html"
...     E.head(
...       E.title("This is a sample document")
...     ),
...     E.body(
...       E.h1("Hello!", CLASS("title")),
...       E.p("This is a paragraph with ", E.b("bold"), " text in it!"),
...       E.p("This is another paragraph, with a", "\n      ",
...         E.a("link", href="http://www.python.org"), "."),
...       E.p("Here are some reservered characters: <spam&egg>."),
...       etree.XML("<p>And finally an embedded XHTML fragment.</p>"),
...     )
...   )
... )

>>> print(etree.tostring(page, pretty_print=True))
<html>
  <head>
    <title>This is a sample document</title>
  </head>
  <body>
    <h1 class="title">Hello!</h1>
    <p>This is a paragraph with <b>bold</b> text in it!</p>
    <p>This is another paragraph, with a
      <a href="http://www.python.org">link</a>.</p>
    <p>Here are some reservered characters: &lt;spam&amp;egg&gt;.</p>
    <p>And finally an embedded XHTML fragment.</p>
  </body>
</html>
时间: 2024-12-05 12:51:25

【python】lxml-The E-factory的相关文章

【python】lxml

来源:http://lxml.de/tutorial.html lxml是python中处理xml的一个非常强大的库,可以非常方便的解析和生成xml文件.下面的内容翻译了链接中的一部分 1.生成空xml节点 from lxml import etree root = etree.Element("root") print(etree.tostring(root, pretty_print=True)) <root/> 2.生成xml子节点 from lxml import

【python】lxml处理命名空间

有如下xml <A xmlns="http://This/is/a/namespace"> <B>dataB1</B> <B>dataB2</B> <B> <C>dataC</C> </B> </A> 其中的xmlns属性表示的是该xml的默认命名空间,该命名空间必须是一个url形式 查看xml的tag #encoding=utf8 from lxml import

【Python】网络编程

1.TCP编程 2.SocketServer模块 3.Twisted框架 4.UDP编程 1.TCP编程--TCP是面向连接的,其一般的设计如下: # encoding:utf-8 ''' Created on 2014-6-20 @author: Administrator ''' from socket import socket, AF_INET, SOCK_STREAM import time import threading class SockServer(object): def

【Python】SyntaxError: Non-ASCII character &#39;\xe8&#39; in file

遇到的第一个问题: SyntaxError: Non-ASCII character '\xe8' in file D:/PyCharmProject/TempConvert.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details 原因:注释里面出现了中文,而 Python 支持的 ASCII 码无中文. 解决方法:在头文件中添加如下代码: # -*- coding:

【python】禁止print输出换行的方法

print后用一个逗号结尾就可以禁止输出换行,例子如下 >>> i=0 >>> while i < 3: print i i+=1 0 1 2 禁止输出换行后效果如下: >>> i=0 >>> while i < 3: print i, i+=1 0 1 2 [python]禁止print输出换行的方法,布布扣,bubuko.com

【python】chr与ord函数的使用

ord()是将已知字母转换成其顺序值: chr()是将已知字母的顺序至转换成其对应的字母 >>> ord("a") 97 >>> ord("A") 65 >>> chr(97) 'a' >>> chr(65) 'A' [python]chr与ord函数的使用,布布扣,bubuko.com

【python】ipython与python的区别

[python]ipython与python的区别 (2014-06-05 12:27:40) 转载▼   分类: Python http://mba.shengwushibie.com/itbook/BookChapter.asp?id=8745 http://www.cnblogs.com/yangze/archive/2011/07/11/2103040.html http://matrix.42qu.com/10735149 http://www.cnblogs.com/weishun/

【python】字符遍历

Python为我们提供了很多便捷的方式去遍历一个字符串中的字符.比如,将一个字符串转换为一个字符数组(列表): theList = list(theString) 同时,我们可以方便的通过for语句进行遍历: for c in theString:        do_something_with(c) map函数用法: 第一个参数接收一个函数名,第二个参数接收一个可迭代对象 lt = [1, 2, 3, 4, 5, 6] def add(num): return num + 1 rs = ma

【Python】用Python的“结巴”模块进行分词

之前都是用计算所的分词工具进行分词,效果不错但是比较麻烦,最近开始用Python的"结巴"模块进行分词,感觉非常方便.这里将我写的一些小程序分享给大家,希望对大家有所帮助. 下面这个程序是对一个文本文件里的内容进行分词的程序:test.py #!/usr/bin/python #-*- encoding:utf-8 -*- import jieba #导入jieba模块 def splitSentence(inputFile, outputFile): fin = open(input

【Python】Python获取命令行参数

有时候需要用同一个Python程序在不同的时间来处理不同的文件,此时如果老是要到Python程序中去修改输入.输出文件名,就太麻烦了.而通过Python获取命令行参数就方便多了.下面是我写得一个小程序,希望对大家有所帮助. 比如下面一个程序test.py是通过接受命令行两个参数,并打印出这两个参数. import sys #需导入sys模块 print sys.argv[1], sys.argv[2] #打印出从命令行接受的两个参数 Linux下运行:python test.py Hello P