V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2015-08-31 18:15:30 +08:00 · 3929 次点击
    这是一个创建于 3132 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个字符串,含有标签。如下所示:
    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello, how can I get to the railway station?</string>

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

    先不说标签发生变化的情况,这个读起来貌似不是很直观...
    adrianzhang
        6
    adrianzhang  
    OP
       2015-08-31 18:46:21 +08:00
    @secondwtq
    @abelyao

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

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

    借一下 2 楼的资料:

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