zoukankan      html  css  js  c++  java
  • 在Linux服务器上安装lxml

    最近新接的活,第一个任务是处理一堆xml格式的专利文件,把里面的有效信息提取出来

    因为公司的相关规定不允许把文件down到本地处理,只能在对方提供的远程服务器上写代码

    由于xml里面的元素是XXX:YYYY这种带前缀的格式,用xml.etree的ElementTree死活解析不出来,最后从OverStack上找到了解释

    ElementTree is not too smart about namespaces. You need to give the .find()findall() and iterfind() methods an explicit namespace dictionary. This is not documented very well:

    namespaces = {'owl': 'http://www.w3.org/2002/07/owl#'} # add more as needed
    
    root.findall('owl:Class', namespaces)

    Prefixes are only looked up in the namespaces parameter you pass in. This means you can use any namespace prefix you like; the API splits off the owl: part, looks up the corresponding namespace URL in the namespaces dictionary, then changes the search to look for the XPath expression {http://www.w3.org/2002/07/owl}Class instead.

    If you can switch to the lxml library things are better; that library supports the same ElementTree API, but collects namespaces for you in a .nsmap attribute on elements.

    于是果断去装lxml

    服务器上已经装了python2.7,然而除此之外的东西全都没有,因此又手动装了pip

    执行pip install lxml 报了一大堆错,注意到:

    Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?

    查到lxml还需要先安装依赖库libxml2和libxslt的开发版

    yum install libxml2-devel

    yum install libxslt-devel

    安装好依赖库后pip还是报错

    这次又认真读了一遍错误信息发现前面居然还有一行:

    unable to execute gcc: No such file or directory

    好吧,连gcc都没有安装,难怪无法编译

    yum install gcc

    安装好以后pip install,居然,还是报错!

    src/lxml/lxml.etree.c:84:20: 致命错误:Python.h:没有那个文件或目录

    再查资料,原来还需要安装一个python-devel,这是Python的头文件和静态库包

    yum install python-devel

    安装完之后再次pip install lxml

    终于见到了喜闻乐见的

    Successfully installed lxml

    至此,linux下的lxml安装完成

    今日事今日毕
  • 相关阅读:
    P2073 送花(Treap维护双权值)
    P2041 [NOI2005]维护数列(Splay树支持插入区间、删除区间、修改区间、翻转区间、区间求和、区间带修改最大子列和的代码模板)
    P1801 黑匣子(Treap树)
    P3377 【模板】左偏树(可并堆)
    P1553 可怜的狗狗(可持久化线段树)
    P1503 鬼子进村(Treap树)
    Adobe CC 2017 全系列官方中文版32/64位
    Serverless简介
    小程序开发框架MPVue和uni-app
    ORM对象关系映射
  • 原文地址:https://www.cnblogs.com/gjack/p/8063564.html
Copyright © 2011-2022 走看看