Skip to content
Advertisement

Name ‘ ‘ is not defined – python

from bunch of files I want to extract some info (code is below), but I came across error: “name ‘GH_Pos21X_true’ is not defined”, full error:

NameError                                 Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_6892/2034911737.py in <module>
     38                 fullname = os.path.join(path, filename)
     39                 #print(fullname)
---> 40                 PN, GH, GH_Design_standard, GH_Design_mirrored, GH_Distance100, GH_Distance101, GH_Studs, GH_Typgruppe_21_23, GH_Typgruppe_22_24, GH_Typgruppe_21_24, GH_Pos21X, GH_Pos21Y, GH_Pos22X, GH_Pos22Y, GH_Pos23X, GH_Pos23Y, GH_Pos24X, GH_Pos24Y, PP, DS, PI, MT_St53Mirror, MT_St53Angle, MT_St73Mounting, MT_TLA = get_data_from_xml(fullname)
     41                 #print(pn, gh, pp)
     42                 pn_list.append(PN)
~AppDataLocalTemp/ipykernel_6892/2435933773.py in get_data_from_xml(path)
     61                             GH_Pos24Y = CompAttrVal.get('Value')
     62 
---> 63                         if GH_Pos21X_true and GH_Pos23X_true:
     64                             GH_Typgruppe_21_23 = 'x'
     65                             GH_Typgruppe_22_24 = ''

NameError: name 'GH_Pos21X_true' is not defined

Where should I defined it?

Code:

def get_data_from_xml(path):
    #print(path)
    
    tree = ET.parse(path)
    root = tree.getroot()
    
    for Type in root.iter('Type'):
        PN = Type.get('name')
        #print(f"Part number: {PN}")
        DS = Type.get('Desc')
        #print(f"Description: {DS}")
        
        for Component in root.iter('Component'):
            CName = Component.get('name')
            if CName == 'Pos010_GearHousing':
                 for ComponentNo in Component.iter('ComponentNo'):
                    GH = ComponentNo.get('name')
                    #print(f"Gear Housing: {GH}")   
                    for CompAttrVal in Component.iter('CompAttrVal'):
                        GH_AttrVal = CompAttrVal.get('name') 
                        if GH_AttrVal == 'GearHousingDesign':
                            GH_Design = CompAttrVal.get('Value')
                            if GH_Design == 'mirrored':
                                GH_Design_standard = ''
                                GH_Design_mirrored = 'x'
                            elif GH_Design == 'standard':
                                GH_Design_standard = 'x'
                                GH_Design_mirrored = ''
                        #GH - ZAM?
                        if GH_AttrVal == 'TieRodDistance':
                            GH_Distance = CompAttrVal.get('Value')
                            if GH_Distance == '100':
                                GH_Distance100 = 'x'
                                GH_Distance101 = ''
                            elif GH_Distance == '101.8':
                                GH_Distance100 = ''
                                GH_Distance101 = 'x'
                        #GH - Riveting Progrs?
                        if GH_AttrVal == 'NumberOfStuds':
                            GH_Studs = CompAttrVal.get('Value')
                        if GH_AttrVal == 'StudPos21X':
                            global GH_Pos21X_true
                            GH_Pos21X = CompAttrVal.get('Value')
                            GH_Pos21X_true = True
                        if GH_AttrVal == 'StudPos21Y':
                            GH_Pos21Y = CompAttrVal.get('Value')
                        if GH_AttrVal == 'StudPos22X':
                            GH_Pos22X = CompAttrVal.get('Value')
                            GH_Pos22X_true = True
                        if GH_AttrVal == 'StudPos22Y':
                            GH_Pos22Y = CompAttrVal.get('Value')
                        if GH_AttrVal == 'StudPos23X':
                            GH_Pos23X = CompAttrVal.get('Value')
                            GH_Pos23X_true = True
                        if GH_AttrVal == 'StudPos23Y':
                            GH_Pos23Y = CompAttrVal.get('Value')
                        if GH_AttrVal == 'StudPos24X':
                            GH_Pos24X = CompAttrVal.get('Value')
                            GH_Pos24X_true = True
                        if GH_AttrVal == 'StudPos24Y':
                            GH_Pos24Y = CompAttrVal.get('Value')
                    
                        if GH_Pos21X_true and GH_Pos23X_true:
                            GH_Typgruppe_21_23 = 'x'
                            GH_Typgruppe_22_24 = ''
                            GH_Typgruppe_21_24 = ''
                        elif GH_Pos22X_true and GH_Pos24X_true:
                            GH_Typgruppe_21_23 = ''
                            GH_Typgruppe_22_24 = 'x'
                            GH_Typgruppe_21_24 = ''
                        elif GH_Pos21X_true and GH_Pos22X_true and GH_Pos23X_true and GH_Pos24X_true:
                            GH_Typgruppe_21_23 = ''
                            GH_Typgruppe_22_24 = ''
                            GH_Typgruppe_21_24 = 'x'
                        else:
                            GH_Typgruppe_21_23 = 'keine studs'
                            GH_Typgruppe_22_24 = ''
                            GH_Typgruppe_21_24 = ''

            if CName == 'Pos058_PowerPack':
                 for ComponentNo in Component.iter('ComponentNo'):
                    PP = ComponentNo.get('name')
                    #print(f"Power Pack: {PP}")
                    
            if CName == 'Pos082_PedalInterface':
                 for ComponentNo in Component.iter('ComponentNo'):
                    PI = ComponentNo.get('name')
                    #print(f"Pedal Interface: {PI}")
                                
            if CName == 'MachineTools':
                 for ComponentNo in Component.iter('ComponentNo'):
                    MT = ComponentNo.get('name')
                    if MT == 'St53_DryTesting':
                         for CompAttrVal in Component.iter('CompAttrVal'):
                            MT_St53 = CompAttrVal.get('name')
                            if MT_St53 == 'TesterECUPlugMirror':
                                MT_St53Mirror = CompAttrVal.get('Value') 
                                #print(f"Mirror: {MT_St53Mirror}")
                            elif MT_St53 == 'TesterECUPlugAngle':
                                MT_St53Angle = CompAttrVal.get('Value')
                                #print(f"Angle: {MT_St53Angle}")           
                    #St. 65 CamCheck???
                    elif MT == 'St73_FirewallGasket':
                        for CompAttrVal in Component.iter('CompAttrVal'):
                            MT_St73 = CompAttrVal.get('name')
                            if MT_St73 == 'Mounting':
                                global MT_St73Mounting
                                MT_St73Mounting = CompAttrVal.get('Value') 
                                #print(f"St73: {MT_St73Mounting}")
            
            if CName == 'Pos088_FirewallGasket':
                for ComponentNo in Component.iter('ComponentNo'):
                    Pos088 = ComponentNo.get('name')
                    if Pos088 == 'MountingPosition':
                        for CompAttrVal in Component.iter('CompAttrVal'):
                            TLA = CompAttrVal.get('name')
                            if TLA == 'ToolLifterAngle':
                                global MT_TLA
                                MT_TLA = CompAttrVal.get('Value') 
                             
            
    #print(PN, GH, PP, DS, PI)
    return PN, GH, GH_Design_standard, GH_Design_mirrored, GH_Distance100, GH_Distance101, GH_Studs, GH_Typgruppe_21_23, GH_Typgruppe_22_24, GH_Typgruppe_21_24, GH_Pos21X, GH_Pos21Y, GH_Pos22X, GH_Pos22Y, GH_Pos23X, GH_Pos23Y, GH_Pos24X, GH_Pos24Y, PP, DS, PI, MT_St53Mirror, MT_St53Angle, MT_St73Mounting, MT_TLA

pn_list = []
gh_list = []
GH_Design_standard_list = []
GH_Design_mirrored_list = []
GH_ZAM_list = []
GH_Distance100_list = []
GH_Distance101_list = []
GH_RivProg_BTR1_list = []
GH_RivProg_BTR2_list = []
GH_Studs_list = []
GH_Typgruppe_21_23_list = []
GH_Typgruppe_22_24_list = []
GH_Typgruppe_21_24_list = []
GH_Pos21X_list = []
GH_Pos21Y_list = []
GH_Pos22X_list = []
GH_Pos22Y_list = []
GH_Pos23X_list = []
GH_Pos23Y_list = []
GH_Pos24X_list = []
GH_Pos24Y_list = []
pp_list = []
ds_list = []
pi_list = []
MT_St53Mirror_list = []
MT_St53Angle_list = []
MT_St65Gasket_list = []
MT_St65Spacer_list = []
MT_St73Mounting_list = []
MT_TLA_list = []

#generowanie DF
path = 'C:/Users/STJ2TW/Desktop/Pliki XML/'
for filename in os.listdir(path):
    if '.' not in filename:
        if 'MASTER' not in filename:
            if 'GHOST' not in filename: 
                fullname = os.path.join(path, filename)
                #print(fullname)
                PN, GH, GH_Design_standard, GH_Design_mirrored, GH_Distance100, GH_Distance101, GH_Studs, GH_Typgruppe_21_23, GH_Typgruppe_22_24, GH_Typgruppe_21_24, GH_Pos21X, GH_Pos21Y, GH_Pos22X, GH_Pos22Y, GH_Pos23X, GH_Pos23Y, GH_Pos24X, GH_Pos24Y, PP, DS, PI, MT_St53Mirror, MT_St53Angle, MT_St73Mounting, MT_TLA = get_data_from_xml(fullname)
                #print(pn, gh, pp)
                pn_list.append(PN)
                gh_list.append(GH)
                GH_Design_standard_list.append(GH_Design_standard)
                GH_Design_mirrored_list.append(GH_Design_mirrored)
                #GH ZAM??
                GH_Distance100_list.append(GH_Distance100)
                GH_Distance101_list.append(GH_Distance101)
                #GH Riveting Progs?
                GH_Studs_list.append(GH_Studs)
                GH_Typgruppe_21_23_list.append(GH_Typgruppe_21_23)
                GH_Typgruppe_22_24_list.append(GH_Typgruppe_22_24)
                GH_Typgruppe_21_24_list.append(GH_Typgruppe_21_24)
                GH_Pos21X_list.append(GH_Pos21X)
                GH_Pos21Y_list.append(GH_Pos21Y)
                GH_Pos22X_list.append(GH_Pos22X)
                GH_Pos22Y_list.append(GH_Pos22Y)
                GH_Pos23X_list.append(GH_Pos23X)
                GH_Pos23Y_list.append(GH_Pos23Y)
                GH_Pos24X_list.append(GH_Pos24X)
                GH_Pos24Y_list.append(GH_Pos24Y)
                pp_list.append(PP)
                ds_list.append(DS)
                pi_list.append(PI)
                MT_St53Mirror_list.append(MT_St53Mirror)
                MT_St53Angle_list.append(MT_St53Angle)
                #St. 65 CamCheck???
                MT_St73Mounting_list.append(MT_St73Mounting)
                MT_TLA_list.append(MT_TLA)
                
a = {'Description': ds_list,
     'Part number': pn_list,
     'Gear Housing': gh_list,
     'Power Pack': pp_list,
     'Pedal Interface': pi_list,
     'ECU PlugMirror': MT_St53Mirror_list,
     'ECU PlugAngle': MT_St53Angle_list,
     'CamProg_Gasket': MT_St65Gasket_list,
     'CamProg_Spacer': MT_St65Spacer_list,
     'Mounting ?': MT_St73Mounting_list,
     'MountingPosition/ToolLifterAngle': MT_TLA_list,
     'Standard': GH_Design_standard_list,
     'Mirrored': GH_Design_mirrored_list,
     'ZAM': GH_ZAM_list,
     '100 Bore Distance': GH_Distance100_list,
     '101,8 Bore Distance': GH_Distance101_list,
     'Program BTR 1': GH_RivProg_BTR1_list,
     'Program BTR 2': GH_RivProg_BTR2_list,
     'Number of Stud Bores': GH_Studs_list,
     '2_Studs_RobPos_21_23': GH_Typgruppe_21_23_list,
     '2_Studs_RobPos_22_24': GH_Typgruppe_22_24_list,
     '4_Studs_RobPos_21-24': GH_Typgruppe_21_24_list,
     'X-Position': GH_Pos21X_list,
     'Y-Position': GH_Pos21Y_list,
     'X-Position': GH_Pos22X_list,
     'Y-Position': GH_Pos22Y_list,
     'X-Position': GH_Pos23X_list,
     'Y-Position': GH_Pos23Y_list,
     'X-Position': GH_Pos24X_list,
     'Y-Position': GH_Pos24Y_list,
     
    }

df = pd.DataFrame.from_dict(a, orient='index')
df = df.transpose()

#zapis DF do pliku
df = df.set_index('Description', drop = True)
df.to_excel("C:/Users/STJ2TW/Desktop/Pliki CSV/data_frames_from_xml.xlsx", startrow=3, freeze_panes=(5,5))

#wczytywanie pliku do edycji
wb = load_workbook(filename = "C:/Users/STJ2TW/Desktop/Pliki CSV/data_frames_from_xml.xlsx")
ws = wb.active
sheet = wb["Sheet1"]

#filtrowanie
ws.insert_rows(5)
sheet.auto_filter.ref = "A5:DE300"
            
#Machine Tools
ws.merge_cells('F3:G3')
ws.merge_cells('H3:I3')
ws.merge_cells('J3:K3')
sheet["F3"].value = "St. 53 DryTest"
sheet["H3"].value = "St. 65 CamCheck"
sheet["J3"].value = "St. 73"

sheet["F4"].value = "ECU PlugMirror"
sheet["G4"].value = "ECU PlugAngle"
sheet["H4"].value = "CamProg_Gasket"
sheet["I4"].value = "CamProg_Spacer"
sheet["J4"].value = "Mounting ?"
sheet.column_dimensions['K'].width = 6
sheet["K4"].value = "MountingPosition/ToolLifterAngle"

#Pos.10 GearHousing
ws.merge_cells('L3:M3')
#N3
ws.merge_cells('O3:P3')
ws.merge_cells('Q3:R3')
#S3
ws.merge_cells('T3:V3')
ws.merge_cells('W3:X3')
ws.merge_cells('Y3:Z3')
ws.merge_cells('AA3:AB3')
ws.merge_cells('AC3:AD3')
sheet["L3"].value = "Design"
sheet["N3"].value = "ZAM"
sheet["O3"].value = "Tierod"
sheet["Q3"].value = "Riveting Progrs."
#S3-empty
sheet["T3"].value = "Typgruppe"
sheet["W3"].value = "RobPos.21"
sheet["Y3"].value = "RobPos.22"
sheet["AA3"].value = "RobPos.23"
sheet["AC3"].value = "RobPos.24"

sheet["L4"].value = "Standard"
sheet["M4"].value = "Mirrored"
sheet["N4"].value = "ZAM"
sheet["O4"].value = "100 Bore Distance"
sheet["P4"].value = "101.8 Bore Distance"
sheet["Q4"].value = "Program BTR 1"
sheet["R4"].value = "Program BTR 2"
sheet["S4"].value = "Number of Stud Bores"
sheet["T4"].value = "2_Studs_RobPos_21_23"
sheet["U4"].value = "2_Studs_RobPos_22_24"
sheet["V4"].value = "4_Studs_RobPos_21-24"
sheet["W4"].value = "X-Position"
sheet["X4"].value = "Y-Position"
sheet["Y4"].value = "X-Position"
sheet["Z4"].value = "Y-Position"
sheet["AA4"].value = "X-Position"
sheet["AB4"].value = "Y-Position"
sheet["AC4"].value = "X-Position"
sheet["AD4"].value = "Y-Position"

#zapis sformatowanego pliku do .xlsx
wb.save("C:/Users/STJ2TW/Desktop/Pliki CSV/data_frames_from_xml_formatted.xlsx")
#df.head(10)

I can’t see a problem, maybe someone could help me find my mistake? I will be grateful.

If I don’t set this variable as global I have this error:

UnboundLocalError                         Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_6892/2034911737.py in <module>
     38                 fullname = os.path.join(path, filename)
     39                 #print(fullname)
---> 40                 PN, GH, GH_Design_standard, GH_Design_mirrored, GH_Distance100, GH_Distance101, GH_Studs, GH_Typgruppe_21_23, GH_Typgruppe_22_24, GH_Typgruppe_21_24, GH_Pos21X, GH_Pos21Y, GH_Pos22X, GH_Pos22Y, GH_Pos23X, GH_Pos23Y, GH_Pos24X, GH_Pos24Y, PP, DS, PI, MT_St53Mirror, MT_St53Angle, MT_St73Mounting, MT_TLA = get_data_from_xml(fullname)
     41                 #print(pn, gh, pp)
     42                 pn_list.append(PN)

~AppDataLocalTemp/ipykernel_6892/1022670823.py in get_data_from_xml(path)
     60                             GH_Pos24Y = CompAttrVal.get('Value')
     61 
---> 62                         if GH_Pos21X_true and GH_Pos23X_true:
     63                             GH_Typgruppe_21_23 = 'x'
     64                             GH_Typgruppe_22_24 = ''

UnboundLocalError: local variable 'GH_Pos21X_true' referenced before assignment

Part of the file – example:

<?xml version="1.0" encoding="UTF-8"?>
<root ReleaseDate="2021082310500904" StartDate="2021082310500904" EndDate="00" Version="1.0.0">
  <Type name="0204N01319-00" Desc="FA VW Golf MIR S1+" OID="73975" SrcOID="73942" TID="3">
    <Component name="MachineTools" OID="99639" SrcOID="24014" TID="8">
      <ComponentNo name="St53_DryTesting" OID="73977" SrcOID="73977" TID="19">
        <CompAttrVal name="TesterECUPlugMirror" OID="73978" SrcOID="73978" TID="10" DataType="String" Value="1"></CompAttrVal>
        <CompAttrVal name="TesterECUPlugAngle" OID="73979" SrcOID="73979" TID="10" DataType="String" Value="0"></CompAttrVal>
      </ComponentNo>
      <ComponentNo name="St73_FirewallGasket" OID="73980" SrcOID="73980" TID="19">
        <CompAttrVal name="Mounting" OID="73981" SrcOID="73981" TID="10" DataType="String" Value="YES"></CompAttrVal>
      </ComponentNo>
    </Component>
    <Component name="Pos010_GearHousing" OID="95697" SrcOID="379" TID="8">
      <ComponentNo name="0204860736" OID="30254" SrcOID="30254" TID="19">
        <CompAttrVal name="GearHousingDesign" OID="30256" SrcOID="30256" TID="10" DataType="String" Value="mirrored"></CompAttrVal>
        <CompAttrVal name="TieRodDistance" OID="30257" SrcOID="30257" TID="10" DataType="String" Value="100"></CompAttrVal>
        <CompAttrVal name="NumberOfStuds" OID="30258" SrcOID="30258" TID="10" DataType="String" Value="2"></CompAttrVal>
        <CompAttrVal name="StudPos21X" OID="30259" SrcOID="30259" TID="10" DataType="String" Value="-13.17"></CompAttrVal>
        <CompAttrVal name="StudPos21Y" OID="30260" SrcOID="30260" TID="10" DataType="String" Value="49.17"></CompAttrVal>
        <CompAttrVal name="StudPos23X" OID="30261" SrcOID="30261" TID="10" DataType="String" Value="13.17"></CompAttrVal>
        <CompAttrVal name="StudPos23Y" OID="30262" SrcOID="30262" TID="10" DataType="String" Value="-49.17"></CompAttrVal>
      </ComponentNo>
    </Component>
    <Component name="Pos012_LabelDMC" OID="97438" SrcOID="380" TID="8">
      <ComponentNo name="0204844713" OID="13172" SrcOID="444" TID="19"></ComponentNo>
    </Component>

Advertisement

Answer

You are using the == comparison operator instead of the assignment operator =

Do this instead

#elif GH_Design == 'standard':
  #GH_Design_standard = 'x'
  #GH_Design_mirrored = ''
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement