Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8572785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:06:17+00:00 2026-06-11T19:06:17+00:00

How do i read the all the text within in the <context>…</context> tag? And

  • 0

How do i read the all the text within in the <context>...</context> tag? And how about the <head>...<\head> tag within the <context \> tag?

I’ve an XML file that looks like this:

<corpus lang="english">
    <lexelt item="coach.n">
        <instance id="1">
            <context>I'll buy a train or <head>coach</head> ticket.</context>
        </instance>
        <instance id="2">
            <context>A branch line train took us to Aubagne where a <head>coach</head> picked us up for the journey up to the camp.</context>
        </instance>
    </lexelt>
</corpus>

But when i ran my code to read the XML text within the …, I’m only getting the text until i reach the tag.

import xml.etree.ElementTree as et    
inputfile = "./coach.data"    
root = et.parse(open(inputfile)).getroot()
instances = []

for corpus in root:
    for lexelt in corpus:
      for instance in lexelt:
        instances.append(instance.text)

j=1
for i in instances:
    print "instance " + j
    print "left: " + i
    print "\n"  
    j+=1

Now I’m just getting the left side:

instance 1
left: I'll buy a train or 

instance 2
left: A branch line train took us to Aubagne where a 

The output needs also the right side of the context and the head, it should be:

instance 1
left: I'll buy a train or 
head: coach
right:   ticket.

instance 2
left: A branch line train took us to Aubagne where a 
head: coach
right:  picked us up for the journey up to the camp.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:06:18+00:00Added an answer on June 11, 2026 at 7:06 pm

    First of all, you have a mistake in your code. for corpus in root is not necessary, your root is already corpus.

    What you probably meant to do was:

    for lexelt in root:
      for instance in lexelt:
        for context in instance:
          contexts.append(context.text)
    

    Now, regarding your question – inside the for context in instance block, you can access the other two strings you need:

    1. The head text can be accessed by accessing context.find('head').text
    2. The text in the right of your head element can be read by accessing context.find('head').tail
      According to the Python etree docs:

    The tail attribute can be used to hold additional data associated with
    the element. This attribute is usually a string but may be any
    application-specific object. If the element is created from an XML
    file the attribute will contain any text found after the element’s end
    tag and before the next tag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to read from a text file in C#. But I want all
Hello to all that read I am self learning C++ from a text book.
This is what my current code looks like: HTML: <a class=button href=#>Read More</a> CSS:
I have this code to read in a text file, with words separated by
I want to read the html file.And for that I use System.IO.File.ReadAllText(path) .It can
I read all of Microsoft's documentation but their claim is that it should work
I read all topics related to this question in stackoverflow and whole internet and
I current have the following XML fragment that I'm parsing to read its values
I have a C# application that simply parses all of the txt documents within
I'm creating a plugin/bookmarklet that will take an XML document embedded within the <textarea>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.