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

  • SEARCH
  • Home
  • 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 8270275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:31:59+00:00 2026-06-08T06:31:59+00:00

2 days ago I was first introduced to Python (and programming in general). Today

  • 0

2 days ago I was first introduced to Python (and programming in general). Today I’m stuck. I’ve spent hours trying to find an answer to what I suspect is a problem so trivial, nobody else has yet been stuck here : )

The boss wants me to manually clean up HUGE .xml files into something more human readable. I’m trying to create a script to do it for me. The following is an example of the .xml file as well as my desired output.

Input (File.xml):

<IssueTracking>
  <Issue>
    <SequenceNum>123</SequenceNum>
    <Subject>Subject of Ticket 123</Subject>
    <Description>Line 1 in Description field of Ticket 123.
Line 2 in Description field of Ticket 123.
Line 3 in Description field of Ticket 123.</Description>
  </Issue>
  <Issue>
    <SequenceNum>124</SequenceNum>
    <Subject>Subject of Ticket 124</Subject>
    <Description>Line 1 in Description field of Ticket 124.
Line 2 in Description field of Ticket 124.
Line 3 in Description field of Ticket 124.</Description>
  </Issue>
</IssueTracking>

Desired Output:

123    Subject of Ticket 123
Line 1 in Description field of Ticket 123.
Line 2 in Description field of Ticket 123.
Line 3 in Description field of Ticket 123.

124    Subject of Ticket 124
Line 1 in Description field of Ticket 124.
Line 2 in Description field of Ticket 124.
Line 3 in Description field of Ticket 124.

Here is what I’ve got so far.

with open(File.xml, 'r') as SourceFile: # Opens the file
    while 1: # Keep going through the file to the end
        SourceFileLine = SourceFile.readline() # Saves lines of the source file
        if not SourceFileLine: # Skip empty lines
            break

        SourceFileLine = SourceFileLine.strip() # Strips the whitespace

        if "<SequenceNum>" in SourceFileLine:
            SequenceNum = SourceFileLine[13:-14]  # Trims the tags, saves the field.
            continue

        if "<Subject>" in SourceFileLine:
            Subject = SourceFileLine[9:-10]
            continue

        #if "<Description>" in SourceFileLine:
        #    last_pos = SourceFile.tell() 
        #    while "</Description>" not in SourceFileLine:
        #        SourceFile.seek(last_pos)
        #        ?????
        #    
        #    Description = Description[22:]
        #    continue

        if "</Issue>" in SourceFileLine:
            print(SequenceNum, end = "\t")
            print(Subject)
        #    print(Description)
            print("\n")

I’m stuck in identifying and retaining those three lines between the <Description> tags into a single string I can print before continuing down the source file. Now having scanned dozens of other examples of file line read loops, I suspect what I need is to flag the point I reach the destination field and nest another read loop at that point in the file. But I have not found another example of this being done, so I assume I’m missing something basic or there is a better way. Thanks in advance for help!

  • 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-08T06:32:00+00:00Added an answer on June 8, 2026 at 6:32 am

    An example of using lxml which I highly recommend to process your data. (nb: written for Py2.x but easy to adapt for Py3.x)

    from lxml import etree
    xml = """<IssueTracking>
      <Issue>
        <SequenceNum>123</SequenceNum>
        <Subject>Subject of Ticket 123</Subject>
        <Description>Line 1 in Description field of Ticket 123.
    Line 2 in Description field of Ticket 123.
    Line 3 in Description field of Ticket 123.</Description>
      </Issue>
      <Issue>
        <SequenceNum>124</SequenceNum>
        <Subject>Subject of Ticket 124</Subject>
        <Description>Line 1 in Description field of Ticket 124.
    Line 2 in Description field of Ticket 124.
    Line 3 in Description field of Ticket 124.</Description>
      </Issue>
    </IssueTracking>
    """
    
    root = etree.fromstring(xml)
    for issue in root.findall('Issue'):
        as_list = [issue.find(n).text for n in ('SequenceNum', 'Subject', 'Description')]
        as_list[2] = as_list[2].split('\n')
        print as_list
    

    Prints:

    ['123', 'Subject of Ticket 123', ['Line 1 in Description field of Ticket 123.', 'Line 2 in Description field of Ticket 123.', 'Line 3 in Description field of Ticket 123.']]
    ['124', 'Subject of Ticket 124', ['Line 1 in Description field of Ticket 124.', 'Line 2 in Description field of Ticket 124.', 'Line 3 in Description field of Ticket 124.']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning Python a couple of days ago as my first language
A few days ago I started with Ubuntu for the first time. I want
I've published my first app 3 days ago (as you see it's my first
I started learning rails a few days ago and I'm reading head first rails
I just started learning SQL a couple of days ago, and I'm trying to
I started programming in Lua few days ago. I have become familiar with the
I'm trying to get number of rows between two dates. First date is today,
A few days ago, this was my question, and I found the answer. Maybe
I have just started android development 3 days ago and I'm trying to develop
I interviewed with Amazon a few days ago. I could not answer one of

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.