Skip to content

Instantly share code, notes, and snippets.

@nicktimko
Created April 25, 2019 18:44
Show Gist options
  • Save nicktimko/aa87a0904c9a7c07725e5edf98a15371 to your computer and use it in GitHub Desktop.
Save nicktimko/aa87a0904c9a7c07725e5edf98a15371 to your computer and use it in GitHub Desktop.
extracting XML titles
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import xml.etree.ElementTree as ET"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"research-article\n",
"A Functional Analysis of the Spacer of V(D)J Recombination Signal Sequences\n"
]
}
],
"source": [
"filepath2=\"journal.pbio.0000001.xml\" \n",
"tree2 = ET.parse(filepath2)\n",
"root2 = tree2.getroot()\n",
"title2=root2.find(\".//article-title\")\n",
"type2=root2.get(\"article-type\")\n",
"print(type2)\n",
"print(title2.text)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"genus-species\n",
"research-article\n",
"None\n"
]
}
],
"source": [
"#file = open(\"all_titles.txt\",\"w\", encoding='utf-8') \n",
"filepath1=\"journal.pbio.0000013.xml\"\n",
"tree = ET.parse(filepath1)\n",
"root = tree.getroot()\n",
"y=root.find(\".//named-content\")\n",
"z=y.get(\"content-type\")\n",
"print(z)\n",
"title=root.find(\".//article-title\")\n",
"type=root.get(\"article-type\")\n",
"print(type)\n",
"print(title.text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://stackoverflow.com/questions/25218358/how-do-i-access-text-between-tags-with-xml-etree-elementtree"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Drosophila', ' Free-Running Rhythms Require Intercellular Communication']"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text_parts = list(title.itertext())\n",
"text_parts"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Drosophila Free-Running Rhythms Require Intercellular Communication'"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\".join(title.itertext())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment