PythonのXML処理モジュール
XMLは「ExtensibleMarkupLanguage」の略です。これは主に、データが特定の構造を持つWebページで使用されます。開始タグと終了タグで定義された要素があります。タグは、<で始まり>で終わるマークアップ構造です。開始タグと終了タグの間の文字は、要素のコンテンツです。要素には、「子要素」と呼ばれる他の要素を含めることができます。
例
以下は、このチュートリアルで使用するXMLファイルの例です。
<?xml version="1.0"?> <Tutorials> <Tutorial id="Tu101"> <author>Vicky, Matthew</author> <title>Geo-Spatial Data Analysis</title> <stream>Python</stream> <price>4.95</price> <publish_date>2020-07-01</publish_date> <description>Learn geo Spatial data Analysis using Python.</description> </Tutorial> <Tutorial id="Tu102"> <author>Bolan, Kim</author> <title>Data Structures</title> <stream>Computer Science</stream> <price>12.03</price> <publish_date>2020-1-19</publish_date> <description>Learn Data structures using different programming lanuages.</description> </Tutorial> <Tutorial id="Tu103"> <author>Sora, Everest</author> <title>Analytics using Tensorflow</title> <stream>Data Science</stream> <price>7.11</price> <publish_date>2020-1-19</publish_date> <description>Learn Data analytics using Tensorflow.</description> </Tutorial> </Tutorials>
xml.etree.ElementTreeを使用したxmlの読み取り
このモジュールは、xmlファイルのルートへのアクセスを提供し、その後、内部要素のコンテンツにアクセスできます。以下の例では、textという属性を使用して、それらの要素のコンテンツを取得します。
例
import xml.etree.ElementTree as ET
xml_tree = ET.parse('E:\\TutorialsList.xml')
xml_root = xml_tree.getroot()
# Header
print('Tutorial List :')
for xml_elmt in xml_root:
for inner_elmt in xml_elmt:
print(inner_elmt.text)
出力
上記のコードを実行すると、次の結果が得られます-
Tutorial List : Vicky, Matthew Geo-Spatial Data Analysis Python 4.95 2020-07-01 Learn geo Spatial data Analysis using Python. Bolan, Kim Data Structures Computer Science 12.03 2020-1-19 Learn Data structures using different programming lanuages. Sora, Everest Analytics using Tensorflow Data Science 7.11 2020-1-19 Learn Data analytics using Tensorflow.
xml属性の取得
ルートタグで属性とその値のリストを取得できます。属性が見つかると、XMLツリーを簡単にナビゲートするのに役立ちます。
例
import xml.etree.ElementTree as ET
xml_tree = ET.parse('E:\\TutorialsList.xml')
xml_root = xml_tree.getroot()
# Header
print('Tutorial List :')
for movie in xml_root.iter('Tutorial'):
print(movie.attrib) 出力
上記のコードを実行すると、次の結果が得られます-
Tutorial List :
{'id': 'Tu101'}
{'id': 'Tu102'}
{'id': 'Tu103'} 結果のフィルタリング
このモジュールのfindall()関数を使用して、xmlツリーから結果をフィルタリングすることもできます。以下の例では、12.03の価格のチュートリアルのIDを見つけます。
例
import xml.etree.ElementTree as ET
xml_tree = ET.parse('E:\\TutorialsList.xml')
xml_root = xml_tree.getroot()
# Header
print('Tutorial List :')
for movie in xml_root.findall("./Tutorial/[price ='12.03']"):
print(movie.attrib) 出力
上記のコードを実行すると、次の結果が得られます-
Tutorial List :
{'id': 'Tu102'} DOMAPIを使用したXMLの解析
xml.domモジュールを使用してminidomオブジェクトを作成します。 minidomオブジェクトは、XMLファイルからDOMツリーをすばやく作成する単純なパーサーメソッドを提供します。サンプルフレーズは、minidomオブジェクトのparse(file [、parser])関数を呼び出して、fileで指定されたXMLファイルをDOMツリーオブジェクトに解析します。
例
from xml.dom.minidom import parse
import xml.dom.minidom
# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse('E:\\TutorialsList.xml')
collection = DOMTree.documentElement
# Get all the movies in the collection
tut_list = collection.getElementsByTagName("Tutorial")
print("*****Tutorials*****")
# Print details of each Tutorial.
for tut in tut_list:
strm = tut.getElementsByTagName('stream')[0]
print("Stream: ",strm.childNodes[0].data)
prc = tut.getElementsByTagName('price')[0]
print("Price: ", prc.childNodes[0].data)
出力
上記のコードを実行すると、次の結果が得られます-
*****Tutorials***** Stream: Python Price: 4.95 Stream: Computer Science Price: 12.03 Stream: Data Science Price: 7.11
-
PythonでのXML解析?
Python XMLパーサーパーサーは、XMLファイルから有用な情報を読み取って抽出する最も簡単な方法の1つを提供します。この短いチュートリアルでは、Python ElementTree XML APIを使用してXMLファイルを解析し、XMLドキュメントを変更および作成する方法を説明します。 Python ElementTree APIは、XMLデータを抽出、解析、変換する最も簡単な方法の1つです。 それでは、ElementTreeを使用してPythonXMLパーサーの使用を開始しましょう。 例1 XMLファイルの作成 まず、要素とサブ要素を含む新しいXMLファイルを作成します。 #Im
-
Pythonでの画像処理?
Pythonには、-を含む画像処理用のライブラリが多数用意されています。 OpenCV −画像処理ライブラリは主にリアルタイムのコンピュータビジョンに焦点を当てており、2Dおよび3D機能ツールキット、顔とジェスチャーの認識、人間とコンピュータの相互作用、モバイルロボット工学、物体識別などの幅広い分野に適用されます。 NumpyおよびScipyライブラリ −画像の操作と処理用。 Sckikit −画像処理用の多くのアルゴリズムを提供します。 Python Imaging Library(PIL) −ツムネイルの作成、サイズ変更、回転、異なるファイル形式間の変換な