我的Python无法正确读取XML

时间:2019-07-09 00:15:37

标签: python xml python-3.x

这是我的python代码访问XML文件:

import xml.etree.ElementTree as ET

def display_book(book):

    root = ET.parse(source="library.xml")

    info = root.iter("catalog")

    for elem in info:
        name = elem.find("book").attrib['id']

        if name == book:
            print(name)

    return "Book Not Found"

#Main
display_book("bk105")

以下是XML代码的示例:

<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>
              An in-depth look at creating applications with XML.
        </description>
    </book>
    <book id="bk102">
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-12-16</publish_date>
        <description>
              A former architect battles corporate zombies, an evil sorceress,                         
              and her own childhood to become queen of the world.
        </description>
    </book>
</catalog>

这些书从“ bk101”到“ bk112”,并且代码仅找到“ bk101”的ID作为第一本书,而不是遍历每个书的ID并将其与输入进行交叉引用,因此程序从不输出任何东西。甚至没有“找不到书”。

2 个答案:

答案 0 :(得分:1)

elem.find("book")仅返回目录中的第一本书。您需要使用elem.findall("book"),然后对其进行迭代。

但是您可以直接使用root.iter("book")遍历书籍。

for elem in root.iter("book"):
    name = elem.attrib('id')
    if name == book:
        print(name)
else:
    print("Book not found")

您还应该将错误消息放在else:块中。否则即使找到该书也将被退回。

答案 1 :(得分:1)

您可以切换为使用diff --git a/pcl-build-for-android.sh b/pcl-build-for-android.sh index f4f1a70..da6ac84 100755 --- a/pcl-build-for-android.sh +++ b/pcl-build-for-android.sh @@ -3,12 +3,11 @@ # specify the arm as abi, the api level for android kitkat as used by # google tango and with gnustl_static the c++ support # for more information look into the android.toolchain.cmake file -export ANDROID_ABI="armeabi armeabi-v7a with NEON" -export ANDROID_NATIVE_API_LEVEL=android-19 -export ANDROID_STL=gnustl_static -export ANDROID_STL_FORCE_FEATURES=ON +ANDROID_ABI="arm64-v8a" +MIN_SDK_VERSION=19 +ANDROID_STL=c++_static -export CFLAGS="-pipe -w" +export CFLAGS="-w" export CXXFLAGS=${CFLAGS} [[ -z "${jobs}" ]] && jobs=$(grep -cP '^processor' /proc/cpuinfo) @@ -25,7 +24,7 @@ fi cd build ROOT=${PWD} -ANDROIDTOOLCHAIN=${ROOT}/../android.toolchain.cmake +ANDROIDTOOLCHAIN=${ANDROID_NDK}/build/cmake/android.toolchain.cmake EIGEN_INCLUDE_DIR=${ROOT}/eigen # eigen needs no cross-compiling FLANN_ROOT=${ROOT}/flann @@ -42,10 +41,23 @@ cmake . -DCMAKE_BUILD_TYPE:STRING=Release \ -DCMAKE_TOOLCHAIN_FILE:FILEPATH=$ANDROIDTOOLCHAIN \ -DBUILD_EXAMPLES:BOOL=OFF \ -DBUILD_PYTHON_BINDINGS:BOOL=OFF \ - -DBUILD_MATLAB_BINDINGS:BOOL=OFF + -DBUILD_MATLAB_BINDINGS:BOOL=OFF \ + -DANDROID_ABI=$ANDROID_ABI \ + -DANDROID_PLATFORM=android-$MIN_SDK_VERSION \ + -DANDROID_STL=$ANDROID_STL \ + +if [ $? -ne 0 ]; then + echo >&2 Build failed + exit 1 +fi make -j${jobs} +if [ $? -ne 0 ]; then + echo >&2 Build failed + exit 1 +fi + cd .. echo "FLANN cross-compiling finished!" @@ -72,10 +84,23 @@ cd ${BOOST_ROOT} cmake . -DCMAKE_BUILD_TYPE:STRING=Release \ -DCMAKE_TOOLCHAIN_FILE:FILEPATH=$ANDROIDTOOLCHAIN \ - -DBUILD_SHARED_LIBS:BOOL=OFF + -DBUILD_SHARED_LIBS:BOOL=OFF \ + -DANDROID_ABI=$ANDROID_ABI \ + -DANDROID_PLATFORM=android-$MIN_SDK_VERSION \ + -DANDROID_STL=$ANDROID_STL \ + +if [ $? -ne 0 ]; then + echo >&2 Build failed + exit 1 +fi make -j${jobs} +if [ $? -ne 0 ]; then + echo >&2 Build failed + exit 1 +fi + cd .. echo "BOOST cross-compiling finished!" @@ -139,13 +164,22 @@ function cmake_pcl { -DBoost_THREAD_LIBRARY=${Boost_LIBRARIES}/libboost_thread.a \ -DBoost_THREAD_LIBRARY_DEBUGBoost_INCLUDE_DIRS=${Boost_LIBRARIES}/libboost_thread.a \ -DBoost_THREAD_LIBRARY_RELEASE=${Boost_LIBRARIES}/libboost_thread.a \ - -DBoost_LIBRARY_DIRS=${Boost_LIBRARIES} + -DBoost_LIBRARY_DIRS=${Boost_LIBRARIES} \ + -DANDROID_ABI=$ANDROID_ABI \ + -DANDROID_PLATFORM=android-$MIN_SDK_VERSION \ + -DANDROID_STL=$ANDROID_STL \ + } ### pcl cmake files do not set library paths properly. Therefore cmake has to be run twice. cmake_pcl cmake_pcl +if [ $? -ne 0 ]; then + echo >&2 Build failed + exit 1 +fi + echo -e "\n\n\033[1;32m make -j$jobs\033[m\n" echo -e "\n\n\033[1;32m this will run for a while... time to drink a\n" echo -e " ( ( " @@ -157,9 +191,19 @@ echo -e " '----' \033[m\n\n" make -j${jobs} +if [ $? -ne 0 ]; then + echo >&2 Build failed + exit 1 +fi + echo -e "\n\n\033[1;35m make install\033[m\n\n" make install +if [ $? -ne 0 ]; then + echo >&2 Build failed + exit 1 +fi + cd .. echo "PCL cross-compiling finished!"

findall