OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Select part of a sentence in a spreadsheet cell to define a condition that modifies the text of a tag in an xml file [duplicate]

  • Thread starter Thread starter Camille
  • Start date Start date
C

Camille

Guest
I have a table like this :

referencesoftware
ABD0H5Dsoftware1, software2, software3
DPJ6R8Gsoftware2, software3
GHI5P6MNaN
AGH7U8Nsoftware1, software3

I would like my code to read the 'software' column and if 'software2' (for example) is present in the cell then the tag text takes the value 'Yes' otherwise 'No' in my xml file whose title contains the same characters as the 'reference' column.

I tried something like :

Code:
import pandas as pd
import xml.etree.ElementTree as ET
import os

table1 = pd.read_excel('C:/Users/Documents/software.xlsx', na_values=['NA'], dtype=str)
table2 = table1.replace('\xa0', ' ',regex=True)

for root, dirs, files in os.walk("."):
    for file in files :
        if file[-4:] == '.xml' and file[:3] == 'LLL': 
            filePath2 = os.path.join(root, file)
            xml = ET.parse(filePath2)
            root = xml.getroot()
            nomenc1 = file[4:10]
            nomenc2 = file[4:11] 
            nomenc3= file[4:12] 
            nomenc4 = file[4:14]
            software_tag = ET.SubElement(root, "tagname")
            softw_excel = table2['software'][table2['reference'].isin([nomenc1, nomenc2, nomenc3, nomenc4])] 
            for softw in softw_excel :
                if softw.str.contains('software2') :
                    software_tag.text = 'Yes'
                else:
                    software_tag.text = 'No'
            ET.indent(root_produit)
            xml.write("infodump.xml", encoding='utf-8', xml_declaration=True, method='xml')

But it sends me back :

Code:
Traceback (most recent call last):

  Cell In[30], line 23
    if softw.str.contains('software2'):

AttributeError: 'float' object has no attribute 'str'

I also tried this :

Code:
syst_excel = table2['software'][table2['reference'].isin([nomenc1, nomenc2, nomenc3, nomenc4])]
S2 = syst_excel.str.contains('software2')
for values in S2 :
    if S2 == True :
        software_tag.text = 'Yes'
    else:
        software_tag.text = 'No'
ET.indent(root) 
xml.write("infodump.xml", encoding='utf-8', xml_declaration=True, method='xml')

I get :

Code:
Traceback (most recent call last):

  Cell In[31], line 26
    if S2 == True :

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I've searched the internet but I can't find my specific problem.
Could you help me, please ?
<p>I have a table like this :</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th>reference</th>
<th>software</th>
</tr>
</thead>
<tbody>
<tr>
<td>ABD0H5D</td>
<td>software1, software2, software3</td>
</tr>
<tr>
<td>DPJ6R8G</td>
<td>software2, software3</td>
</tr>
<tr>
<td>GHI5P6M</td>
<td>NaN</td>
</tr>
<tr>
<td>AGH7U8N</td>
<td>software1, software3</td>
</tr>
</tbody>
</table></div>
<p>I would like my code to read the 'software' column and if 'software2' (for example) is present in the cell then the tag text takes the value 'Yes' otherwise 'No' in my xml file whose title contains the same characters as the 'reference' column.</p>
<p>I tried something like :</p>
<pre><code>import pandas as pd
import xml.etree.ElementTree as ET
import os

table1 = pd.read_excel('C:/Users/Documents/software.xlsx', na_values=['NA'], dtype=str)
table2 = table1.replace('\xa0', ' ',regex=True)

for root, dirs, files in os.walk("."):
for file in files :
if file[-4:] == '.xml' and file[:3] == 'LLL':
filePath2 = os.path.join(root, file)
xml = ET.parse(filePath2)
root = xml.getroot()
nomenc1 = file[4:10]
nomenc2 = file[4:11]
nomenc3= file[4:12]
nomenc4 = file[4:14]
software_tag = ET.SubElement(root, "tagname")
softw_excel = table2['software'][table2['reference'].isin([nomenc1, nomenc2, nomenc3, nomenc4])]
for softw in softw_excel :
if softw.str.contains('software2') :
software_tag.text = 'Yes'
else:
software_tag.text = 'No'
ET.indent(root_produit)
xml.write("infodump.xml", encoding='utf-8', xml_declaration=True, method='xml')
</code></pre>
<p>But it sends me back :</p>
<pre><code>Traceback (most recent call last):

Cell In[30], line 23
if softw.str.contains('software2'):

AttributeError: 'float' object has no attribute 'str'
</code></pre>
<p>I also tried this :</p>
<pre><code>syst_excel = table2['software'][table2['reference'].isin([nomenc1, nomenc2, nomenc3, nomenc4])]
S2 = syst_excel.str.contains('software2')
for values in S2 :
if S2 == True :
software_tag.text = 'Yes'
else:
software_tag.text = 'No'
ET.indent(root)
xml.write("infodump.xml", encoding='utf-8', xml_declaration=True, method='xml')
</code></pre>
<p>I get :</p>
<pre><code>Traceback (most recent call last):

Cell In[31], line 26
if S2 == True :

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
</code></pre>
<p>I've searched the internet but I can't find my specific problem.<br />
Could you help me, please ?</p>
 

Latest posts

Top