0

please i need some help with Python. i have the folowwing result XML.

bloc_elements = soup.find_all('bloc')

RESULT =>

print(bloc_elements)

<Bloc>
<a>data_a</a>
<b>data_b</b>
<c>data_c</c>
<d>data_d</d>
</Bloc>

am trying to get this result :

List into CSV file : 
a       b       c        d  
data_a  data_b  data_c   data_d

this is what i have done:

elements_list = []
for element in bloc_elements:
    elements_list.append(element.get_text())

print(elements_list)
am getting infortunatly this result:

data_a
data_b
data_c
data_d

Problems:

  • I lost the tag (label of the line )
  • the results are in vertical not horizontally

thank you so much for your help

1 Answers1

0

A simple list and dict comprehension does it. Clearly you can use to_csv() to create a CSV from the data frame.

from bs4 import BeautifulSoup
xml = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Bloc>
<a>data_a</a>
<b>data_b</b>
<c>data_c</c>
<d>data_d</d>
</Bloc>"""
soup = BeautifulSoup(xml, "lxml")
df = pd.DataFrame([{ee.name: ee.text for ee in e.find_all(recursive=False)} for e in soup.find_all("bloc")  ])

output

      a       b       c       d
 data_a  data_b  data_c  data_d
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • Hello Rob, Thank you so much i ve tried it on my code and really it works... have more things to learn in Python...thank you again. just for my knowledge. {ee.name: ee.text for ee in e.find_all(recursive=False)} this part of code is done to grab the labels ? what is the recursive function? Thank you so much – The Swayly Jan 09 '21 at 14:50
  • it's not recursive, only want direct descendants https://www.crummy.com/software/BeautifulSoup/bs4/doc/#the-recursive-argument – Rob Raymond Jan 09 '21 at 17:55
  • Okay , i got it . thnk you again Rob – The Swayly Jan 10 '21 at 00:41
  • Hello Rob, please :) can you help me with this issue ?? thank you , its in the same file. https://stackoverflow.com/questions/65748393/beautifulsoup-parsing-xml-to-table – The Swayly Jan 16 '21 at 12:04