V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
adrianzhang
V2EX  ›  Python

怎么样从一个含有 xml 标签的字符串中截取内容

  •  
  •   adrianzhang · Aug 31, 2015 · 4846 views
    This topic created in 3891 days ago, the information mentioned may be changed or developed.
    一个字符串,含有标签。如下所示:
    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello, how can I get to the railway station?</string>

    问题:
    这个内容(本例中是:“ Hello .... station?”)是变化的。
    想只把内容取出来,也就是不要那些标签, Python 好像没有什么简单的办法?大家有没有好主意,一两条语句搞定,最好只使用标准库。
    25 replies    2015-09-03 08:24:46 +08:00
    abelyao
        1
    abelyao  
       Aug 31, 2015
    正则表达式…
    adrianzhang
        3
    adrianzhang  
    OP
       Aug 31, 2015
    @abelyao
    @seki
    谢谢两位帮忙。我想到了一个简单的办法:取这个字符串的[68:-9]
    abelyao
        4
    abelyao  
       Aug 31, 2015
    @adrianzhang 如果确定标签、以及标签属性都是固定的,那这样确实是最方便的
    secondwtq
        5
    secondwtq  
       Aug 31, 2015
    @adrianzhang 总感觉这样好奇怪...

    先不说标签发生变化的情况,这个读起来貌似不是很直观...
    adrianzhang
        6
    adrianzhang  
    OP
       Aug 31, 2015
    @secondwtq
    @abelyao

    标签不变。想了很多方法,解析 xml 啦,解析 html 啦,等等,都好复杂。只有这方法能一条语句解决问题。
    abelyao
        7
    abelyao  
       Aug 31, 2015
    @adrianzhang 正则也是一句搞定,思路就是取第一个 <> 开始的内容,到最后一个 </> 结束之前。
    iniwap
        8
    iniwap  
       Aug 31, 2015
    。。。太不优雅了
    firemiles
        9
    firemiles  
       Aug 31, 2015   ❤️ 1
    str1.replace ('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">','').replace ('</string>','')
    是不是更好看点
    adrianzhang
        10
    adrianzhang  
    OP
       Aug 31, 2015
    @abelyao show me the code?
    @firemiles 嗯嗯,好看!我去 test test 。
    @iniwap 给个优雅的办法?
    em70
        11
    em70  
       Aug 31, 2015
    #提取任意两个字符串之间的内容
    def txt_wrap_by (html,start_str, end,y ):
    start = html.find (start_str )
    if start >= 0:
    start += len (start_str )
    end = html.find (end, start )
    if end >= 0:
    return html[start:end].strip ()
    adrianzhang
        12
    adrianzhang  
    OP
       Aug 31, 2015
    @firemiles 在双引号前加反斜杆转义一下就 OK 了。这个写法真好!

    @em70 嗯嗯,谢谢。想用一两句语句解决这问题。
    abelyao
        13
    abelyao  
       Aug 31, 2015 via iPhone
    @adrianzhang 思路都说了还 show me the code 呵呵
    em70
        14
    em70  
       Aug 31, 2015
    @adrianzhang 函数都给你定义了,不就是 txt_wrap_by 一行代码搞定了吗,python 没有这种内置函数,PHP 有,要不你换个语言?
    adrianzhang
        15
    adrianzhang  
    OP
       Aug 31, 2015
    @em70 明白了。谢谢。
    zhicheng
        16
    zhicheng  
       Aug 31, 2015 via Android   ❤️ 4
    烂代码和烂系统就是这么来的。
    secondwtq
        17
    secondwtq  
       Aug 31, 2015   ❤️ 2
    感觉有时候总是要 oneliner 不是什么好习惯呢。

    借一下 2 楼的资料:

    import xml.etree.ElementTree
    xml.etree.ElementTree.fromstring ('<somestring>').text
    hsyu53
        18
    hsyu53  
       Aug 31, 2015 via Android
    lxml 专干个事的
    bbking
        19
    bbking  
       Aug 31, 2015
    xml.dom, lxml 都可以
    adrianzhang
        20
    adrianzhang  
    OP
       Aug 31, 2015
    @secondwtq 原来是这样实现?!没提问题之前看这个库看得头晕。终于找到我认为的最优解了。多谢! 10 个铜板奉上!
    racal
        21
    racal  
       Sep 1, 2015 via Android
    用正则轻松解决
    TimePPT
        22
    TimePPT  
    PRO
       Sep 1, 2015
    Beautiful Soup 你值得拥有
    TimePPT
        23
    TimePPT  
    PRO
       Sep 1, 2015
    好吧,没仔细审题,最好标准库...
    guoqiao
        24
    guoqiao  
       Sep 3, 2015
    关于这个问题, 如果只是一次性的, 我目前见到的最有创意的方法, 是在浏览器的控制台里, 用 jQuery 的选择器, 一行代码搞定. jQuery 虽然是针对 html 设计的, 但是在 xml 上一样好用.
    即使不是一次性的, 也可以考虑用 Phantomjs + JQuery 等 JS 解决方案, 应当是最优雅的.
    说 BS, PyQuery, lxml 的各位, 且不说这个几个库是否好用, 光是安装它们时的依赖问题, 就够让人烦的.
    adrianzhang
        25
    adrianzhang  
    OP
       Sep 3, 2015
    @guoqiao 这是 python 程序编写过程中的一个小坎坷,用 jQuery 岂不是要安装相关的东东?我认为比较好的那个回复,采用的是标准库,不需要安装什么的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6057 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 59ms · UTC 02:12 · PVG 10:12 · LAX 19:12 · JFK 22:12
    ♥ Do have faith in what you're doing.