質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

621閲覧

先程の質問と似ているがxmlファイルのデータの抽出

123478

総合スコア2

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/09/30 07:33

編集2022/09/30 09:06

<passage> <infon key="section_type">ABSTRACT</infon> <infon key="type">abstract</infon> <offset>81</offset> <text>Neurologic complications of COVID-19 infection have been recently described and include dizziness, headache, loss of taste and smell, stroke, and encephalopathy. Brain MRI in these patients have revealed various findings including ischemia, hemorrhage, inflammation, and demyelination. In this article, we report a case of critical illness-associated cerebral microbleeds identified on MRI in a patient with severe COVID-19 infection and discuss the potential etiologies of these neuroimaging findings.</text>  <annotation id="20">
<infon key="identifier">9606</infon>
<infon key="type">Species</infon>
<location offset="262" length="8"/>
<text>patients</text>              
<passage>

ソースコード
import xml.etree.ElementTree as ET
tree = ET.parse('result.xml')
root=tree.getroot()

試したこと

pythonでxmlファイルのannotationのtextの部分を無視してNeurologicの部分のtextの抽出って可能なのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

BioC XML の場合、sentencerelation タグが含まれる場合があります。例えば以下の様にです。

result.xml

xml

1<passage> 2 <infon key="section_type">ABSTRACT</infon> 3 <infon key="type">abstract</infon> 4 <offset>81</offset> 5 <text>Neurologic complications of COVID-19 ... </text> 6 <annotation id="20"> 7 <infon key="identifier">9606</infon> 8 <infon key="type">Species</infon> 9 <location offset="262" length="8"/> 10 <text>patients</text> 11 </annotation> 12 <sentence> 13 <infon key="type">original sentence</infon> 14 <offset>70</offset> 15 <text>Active Raf-1 phosphorylates and activates the mitogen-activated protein ... </text> 16 </sentence> 17</passage>

以下は lxml - Processing XML and HTML with Python モジュールを使う場合です。

python

1from lxml import etree as ET 2 3tree = ET.parse('result.xml') 4root = tree.getroot() 5texts = [t.text for t in root.findall('.//text') if t.getparent().tag != 'annotation'] 6print(texts) 7 8# 9['Neurologic complications of COVID-19 ...', 'Active Raf-1 phosphorylates and activates the mitogen-activated protein ...']

投稿2022/09/30 11:25

melian

総合スコア19714

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

123478

2022/09/30 12:02

result.xml <passage> <infon key="authors">Gupta NA, Lien C, Iv M, </infon> <offset>0</offset> <text>Critical illness-associated cerebral microbleeds in severe COVID-19 infection </text> <infon key="section_type">ABSTRACT</infon> <infon key="type">abstract</infon> <offset>81</offset> <text>Neurologic complications of COVID-19 ... </text> <annotation id="20"> <infon key="identifier">9606</infon> <infon key="type">Species</infon> <location offset="262" length="8"/> <text>patients</text> </annotation> <passage> 先程のコードだとCritical illness-associatedの文が含まれてしまうんですがどうするればよいでしょうか.
shiracamus

2022/09/30 12:06 編集

そのxml、最後の<passage>は</passage>なのでは? 動作確認したxmlでしょうか?
123478

2022/09/30 12:08

</passsage>ですね.ダウンロードした容量の大きいXMLファイルの一部分を切り取ったものになります.
melian

2022/09/30 12:18

> 先程のコードだとCritical illness-associatedの文が含まれてしまうんですが annotation タグの外側にありますので含まれることになります。
123478

2022/09/30 12:26

そうなんですね.ありがとうございます.
guest

0

result.xml

1<passage> 2 <infon key="section_type">ABSTRACT</infon> 3 <infon key="type">abstract</infon> <offset>81</offset> 4 <text>Neurologic complications of COVID-19 infection have been recently described and include dizziness, headache, loss of taste and smell, stroke, and encephalopathy. Brain MRI in these patients have revealed various findings including ischemia, hemorrhage, inflammation, and demyelination. In this article, we report a case of critical illness-associated cerebral microbleeds identified on MRI in a patient with severe COVID-19 infection and discuss the potential etiologies of these neuroimaging findings.</text> 5 <annotation id="20"> 6 <infon key="identifier">9606</infon> 7 <infon key="type">Species</infon> 8 <location offset="262" length="8"/> 9 <text>patients</text> 10 </annotation> 11</passage>

py

1import xml.etree.ElementTree as ET 2 3tree = ET.parse('result.xml') 4root = tree.getroot() 5text = root.find('.//text') 6print("textがありません" if text is None else text.text)

text:実行結果

1Neurologic complications of COVID-19 infection have been recently described and include dizziness, headache, loss of taste and smell, stroke, and encephalopathy. Brain MRI in these patients have revealed various findings including ischemia, hemorrhage, inflammation, and demyelination. In this article, we report a case of critical illness-associated cerebral microbleeds identified on MRI in a patient with severe COVID-19 infection and discuss the potential etiologies of these neuroimaging findings.

投稿2022/09/30 08:41

編集2022/09/30 09:44
shiracamus

総合スコア5406

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

123478

2022/09/30 09:12

import xml.etree.ElementTree as ET tree = ET.parse('result.xml') root=tree.getroot() print(root.find('text').text) これだとAttributeError: 'NoneType' object has no attribute 'text' というエラーが出てしまうのですがどうすればよいでしょうか
shiracamus

2022/09/30 09:42 編集

上記のxmlはタグが不完全なのでxmlを修正しました。 示されたコードを実行してみましたが、表示されましたよ。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問