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 9210223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:01:15+00:00 2026-06-18T01:01:15+00:00

I have an XML file with several thousand records in it in the form

  • 0

I have an XML file with several thousand records in it in the form of:

<custs>
    <record cust_ID="B123456@Y1996" l_name="Jungle" f_name="George" m_name="OfThe" city="Fairbanks" zip="00010" current="1" />
    <record cust_ID="Q975697@Z2000" l_name="Freely" f_name="I" m_name="P" city="Yellow River" zip="03010" current="1" />
    <record cust_ID="M7803@J2323" l_name="Jungle" f_name="Jim" m_name="" city="Fallen Arches" zip="07008" current="0" />
</custs>
    #   (I know it's not normalized.  This is just sample data)

How can I convert this into a CSV or tab-delimited file? I know I can hard-code it in Python using re.compile() statements, but there has to be something easier, and more portable among diff XML file layouts.

I’ve found a couple threads here about attribs, (Beautifulsoup unable to extract data using attrs=class, Extracting an attribute value with beautifulsoup) and they have gotten me almost there with:

#  Python 3.30
#
from bs4 import BeautifulSoup
import fileinput

Input = open("C:/Python/XML Tut/MinGrp.xml", encoding = "utf-8", errors = "backslashreplace")
OutFile = open('C:/Python/XML Tut/MinGrp_Out.ttxt', 'w', encoding = "utf-8", errors = "backslashreplace")

soup = BeautifulSoup(Input, features="xml")

results = soup.findAll('custs', attrs={})

# output = results   [0]#[0]

for each_tag in results:
    cust_attrb_value = results[0]
#       print (cust_attrb_value)
    OutFile.write(cust_attrb_value)

OutFile.close()

What’s the next (last?) step?

  • 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-18T01:01:16+00:00Added an answer on June 18, 2026 at 1:01 am

    I (also) wouldn’t use BeautifulSoup for this, and though I like lxml, that’s an extra install, and if you don’t want to bother, this is simple enough to do with the standard lib ElementTree module.

    Something like:

    import xml.etree.ElementTree as ET
    import sys
    tree=ET.parse( 'test.xml' )
    root=tree.getroot()
    rs=root.getchildren()
    keys = rs[0].attrib.keys()
    for a in keys: sys.stdout.write(a); sys.stdout.write('\t')
    sys.stdout.write('\n')
    for r in rs:
        assert keys == r.attrib.keys()
        for k in keys: sys.stdout.write( r.attrib[k]); sys.stdout.write('\t')
        sys.stdout.write('\n')
    

    will, from python-3, produce :

    zip m_name  current city    cust_ID l_name  f_name  
    00010   OfThe   1   Fairbanks   B123456@Y1996   Jungle  George  
    03010   P   1   Yellow River    Q975697@Z2000   Freely  I   
    07008       0   Fallen Arches   M7803@J2323 Jungle  Jim 
    

    Note that with Python-2.7, the order of the attributes will be different.
    If you want them to output in a different specific order, you should sort or
    order the list “keys” .

    The assert is checking that all rows have the same attributes.
    If you actually have missing or different attributes in the elements,
    then you’ll have to remove that and add some code to deal with the differences
    and supply defaults for missing values. ( In your sample data, you have a
    null value ( m_name=”” ), rather than a missing value. You might want to check
    that this case is handled OK by the consumer of this output, or else add some
    more special handling for this case.

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

Sidebar

Related Questions

I have several records from the DB for a corresponding record in a file.
I'm trying to have a script automatically transform an xml file into several html
Suppose i have a xml file which has several nodes & children. I am
In my layout xml file I have several buttons like this which share the
I have a class which reads an XML file to create several screens in
I have an XML file with several child elements. Each child element contains a
What I have done is created an XML file with a list of several
I have an XML drawable file with a stroke, and I also have several
I have several items in my xml file, but only one and the first
I have several medium sized (1 MB) xml file that I frequently need to

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.