Note that there are some explanatory texts on larger screens.

plurals
  1. POExtracting XML Attributes
    primarykey
    data
    text
    <p>I have an XML file with several thousand records in it in the form of:</p> <pre><code>&lt;custs&gt; &lt;record cust_ID="B123456@Y1996" l_name="Jungle" f_name="George" m_name="OfThe" city="Fairbanks" zip="00010" current="1" /&gt; &lt;record cust_ID="Q975697@Z2000" l_name="Freely" f_name="I" m_name="P" city="Yellow River" zip="03010" current="1" /&gt; &lt;record cust_ID="M7803@J2323" l_name="Jungle" f_name="Jim" m_name="" city="Fallen Arches" zip="07008" current="0" /&gt; &lt;/custs&gt; # (I know it's not normalized. This is just sample data) </code></pre> <p>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.</p> <p>I've found a couple threads here about attribs, (<a href="https://stackoverflow.com/questions/11713977/beautifulsoup-unable-to-extract-data-using-attrs-class">Beautifulsoup unable to extract data using attrs=class</a>, <a href="https://stackoverflow.com/questions/2612548/extracting-an-attribute-value-with-beautifulsoup">Extracting an attribute value with beautifulsoup</a>) and they have gotten me almost there with:</p> <pre><code># 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() </code></pre> <p>What's the next (last?) step?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload