3

This question already has an answer here:

*Updated Question at bottom Update #3

I need to label chemical formulas out of the attribute table but the attribute table doesn't recognize anything but plain text.

Therefore I have built a dictionary containing each formula so Arc will know how to format each formula,

Issue #1 BUT only the first chemical formula formats correctly and the rest prints as plain text. Does anyone know how to fix this? This has been fixed, please continue to Update #3

Below is an example of my code.

NOTE: my Feature is Wells and its labeling field is GWMW_NAME. The field is printed as such [Wells.GWMW_NAME] as I have joined my shape with another table to join the chemical formula data I need to label.

My chemical formula data is in the following fields: [Exceedance Figure Data - FOXLF.csv.SPRING_EXC] & [Exceedance Figure Data - FOXLF.csv.FALL_EXC]

These fields contain my formulas which are in brackets and are separated by a comma as such: (EC, HCO3-, CaCO3, Ca, OH-, Mg, K, SO42-, TDS, Mn, Sr)

Here is a snip from my attribute table:

enter image description here

def FindLabel ( [Wells.GWMW_NAME] , [Exceedance Figure Data - FOXLF.csv.SPRING_EXC] , [Exceedance Figure Data - FOXLF.csv.FALL_EXC]   ):
formats = {
    'SiO2': 'SiO<sub>2</sub>',
    'CO32-': 'CO<sub>3</sub><sup>2-</sup>',
    'HCO3-': 'HCO<sub>3</sub><sup>-</sup>',
    'CaCO3': 'CaCO<sub>3</sub>',
    'NH3': 'NH<sub>3</sub>',
    'OH-': 'OH<sup>-</sup>',
    'NO2-': 'NO<sub>2</sub><sup>-</sup>',
    'SO42-': 'SO<sub>4</sub><sup>2-</sup>'
}
formula1 = [Exceedance Figure Data - FOXLF.csv.SPRING_EXC].strip('()').split(',')
formula2 = [Exceedance Figure Data - FOXLF.csv.FALL_EXC].strip('()').split(',')
label1 = ','.join([formats.get(f, f) for f in formula1])
label2 = ','.join([formats.get(f, f) for f in formula2])

return "{}\r\n <FNT scale='65'><CLR red='255' green='0' blue='255'>({})</CLR>\r\n<CLR red='0' green='122' blue='192'>({})</CLR></FNT>".format( [Wells.GWMW_NAME] , label1,label2 )

Below is a snip from what my code produces, note the properly formatted formulas circled in green, and the subsequent formulas all print as plain text (I circled some in red to show which did not label correctly).

Can anyone help me fix this so all of my chemical formulas will format and not just the first? This has been fixed, please go to Update #3 for newest issue

Example

Update #1 I've modified my code to try and incorperate Bera's code into mine as seen below**, however I am getting an error of 'Syntax error: 'return' outside function (,string>,line 19).

def FindLabel ( [Wells.GWMW_NAME],  [Exceedance Figure Data - FOXLF.csv.SPRING_EXC] , [Exceedance Figure Data - FOXLF.csv.FALL_EXC]  ):
a = [Exceedance Figure Data - FOXLF.csv.SPRING_EXC]
b = [Exceedance Figure Data - FOXLF.csv.FALL_EXC]
formats = {
    'SiO2': 'SiO<sub>2</sub>',
    'CO32-': 'CO<sub>3</sub><sup>2-</sup>',
    'HCO3-': 'HCO<sub>3</sub><sup>-</sup>',
    'CaCO3': 'CaCO<sub>3</sub>',
    'NH3': 'NH<sub>3</sub>',
    'OH-': 'OH<sup>-</sup>',
    'NO2-': 'NO<sub>2</sub><sup>-</sup>',
    'SO42-': 'SO<sub>4</sub><sup>2-</sup>'
}    
c = [i.strip(',') for i in a[1:-1].split()]
d = [i.strip(',') for i in b[1:-1].split()]
e = [formats.get(i,i) for i in c]    
f = [formats.get(i,i) for i in d]  
return "{}\r\n <FNT scale='65'><CLR red='255' green='0' blue='255'>({})</CLR>\r\n<CLR red='0' green='122' blue='192'>({})</CLR></FNT>".format( [Wells.GWMW_NAME] ,e ,f )

This has been fixed through comments, please go to update #3 for most recent issue

Update #2 I have fixed the indent as per Matt's recommendation, however now I have correct subscript formatting but every formatted chemical formula is being placed inside of ' '. The total line of formulas which is supposed to be in regular brackets are also now inside square brackets. I'm not sure why these extra characters andf ' ' are being added in, especially as I'm not asking for square brackets anywhere. Snip below

enter image description here And here is the code which produced this snip. Update #2 issue fixed via comments from Matt

def FindLabel ( [Wells.GWMW_NAME],  [Separate Exceedance Figure Data - FOXLF.csv.SPRING_EXC] , [Separate Exceedance Figure Data - FOXLF.csv.FALL_EXC] ):
    a = [Separate Exceedance Figure Data - FOXLF.csv.SPRING_EXC]
    b = [Separate Exceedance Figure Data - FOXLF.csv.FALL_EXC]
    formats = {
        'SiO2': 'SiO<sub>2</sub>',
        'CO32-': 'CO<sub>3</sub><sup>2-</sup>',
        'HCO3-': 'HCO<sub>3</sub><sup>-</sup>',
        'CaCO3': 'CaCO<sub>3</sub>',
        'NH3': 'NH<sub>3</sub>',
        'OH-': 'OH<sup>-</sup>',
        'NO2-': 'NO<sub>2</sub><sup>-</sup>',
        'SO42-': 'SO<sub>4</sub><sup>2-</sup>'
    }    
    c = [str(i.strip(',')) for i in a[1:-1].split()]
    d = [str(i.strip(',')) for i in b[1:-1].split()
    e = [formats.get(i,i) for i in c]    
    f = [formats.get(i,i) for i in d]  
    return "{}\r\n <FNT scale='65'><CLR red='255' green='0' blue='255'>({})</CLR>\r\n<CLR red='0' green='122' blue='192'>({})</CLR></FNT>".format( [Wells.GWMW_NAME] ,e ,f )

Update #3 I got my code to work perfectly, UNTIL I needed to make a small edit on my map - now arc is throwing me all kind of unexpected errors and I can't get my code to work anymore. Most recently, it will label one symbol/label class properly, then fail for the other class from the same feature.. so bizarre and its delaying my finished jobs!

Whats even more bizarre is that I tried exporting a shape after joining my original shape with the exceedance table data (just in case the table join was causing the python errors). Once I did this and corrected the fields, wells with no FALL or SPRING_EXC values should label just the [GWMW_NAME] (as per my elif at the end of my code) but instead it also ended up labeling empty brackets below the [GWMW_NAME] (which I do not ask for if [SPRING_EXC] and [FALL_EXC] both == None). So the same code is producing multiple outputs and sometimes just returning errors.

A snip of the error I am getting is below with my most up to date code: Current Error This is the error I am getting.

Here is the code producing said error:

def FindLabel ( [WellShape.GWMW_NAME]  ,  [2018ExceedanceFigureData-SGPLF_mhuber2.csv.SPRING_EXC], [2018ExceedanceFigureData-SGPLF_mhuber2.csv.FALL_EXC] ):
    a = [2018ExceedanceFigureData-SGPLF_mhuber2.csv.SPRING_EXC]    
    b = [2018ExceedanceFigureData-SGPLF_mhuber2.csv.FALL_EXC]    
    formats = {
        'SiO2': 'SiO<sub>2</sub>',
        'CO32-': 'CO<sub>3</sub><sup>2-</sup>',
        'HCO3-': 'HCO<sub>3</sub><sup>-</sup>',
        'CaCO3': 'CaCO<sub>3</sub>',
        'NH3': 'NH<sub>3</sub>',
        'NO3-': 'NO<sub>3</sub><sup>-</sup>',
        'NO2-': 'NO<sub>2</sub><sup>-</sup>',
        'NO3-+NO2-': 'NO<sub>3</sub><sup>-</sup>+NO<sub>2</sub><sup>-</sup>',
        'OH-': 'OH<sup>-</sup>',
        'NO2-': 'NO<sub>2</sub><sup>-</sup>',
        'SO42-': 'SO<sub>4</sub><sup>2-</sup>'
    }    
    c = [str(i.strip(',')) for i in a[1:-1].split()]
    d = [str(i.strip(',')) for i in b[1:-1].split()]
    e = ','.join([formats.get(i,i) for i in c])    
    f = ','.join([formats.get(i,i) for i in d])
    if a == None and b != None :
        return "{}\r\n <FNT scale='65'><CLR red='0' green='122' blue='192'>({})</CLR></FNT>".format( [WellShape.GWMW_NAME] ,f )
    elif a != None and b == None:
        return "{}\r\n <FNT scale='65'><CLR red='255' green='0' blue='255'>({})</CLR></FNT>".format( [WellShape.GWMW_NAME] ,e )
    elif a != None and b != None:
        return "{}\r\n <FNT scale='65'><CLR red='255' green='0' blue='255'>({})</CLR>\r\n<CLR red='0' green='122' blue='192'>({})</CLR></FNT>".format( [WellShape.GWMW_NAME] ,e ,f )
    elif a == None and b == None:
        return [WellShape.GWMW_NAME] 

marked as duplicate by user2856, whyzar, Vince, Jochen Schwarze, Barbarossa Jan 24 at 20:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

All the code that should be inside the function must be indented in Python.

def FindLabel ( [Wells.GWMW_NAME],  [Exceedance Figure Data - FOXLF.csv.SPRING_EXC] , [Exceedance Figure Data - FOXLF.csv.FALL_EXC]  ):
    a = [Exceedance Figure Data - FOXLF.csv.SPRING_EXC]
    b = [Exceedance Figure Data - FOXLF.csv.FALL_EXC]
    formats = {
        'SiO2': 'SiO<sub>2</sub>',
        'CO32-': 'CO<sub>3</sub><sup>2-</sup>',
        'HCO3-': 'HCO<sub>3</sub><sup>-</sup>',
        'CaCO3': 'CaCO<sub>3</sub>',
        'NH3': 'NH<sub>3</sub>',
        'OH-': 'OH<sup>-</sup>',
        'NO2-': 'NO<sub>2</sub><sup>-</sup>',
        'SO42-': 'SO<sub>4</sub><sup>2-</sup>'
    }    
    c = [i.strip(',') for i in a[1:-1].split()]
    d = [i.strip(',') for i in b[1:-1].split()]
    e = [formats.get(i,i) for i in c]    
    f = [formats.get(i,i) for i in d]  
    return "{}\r\n <FNT scale='65'><CLR red='255' green='0' blue='255'>({})</CLR>\r\n<CLR red='0' green='122' blue='192'>({})</CLR></FNT>".format( [Wells.GWMW_NAME] ,e ,f )
  • Thank you! However while this fixes my issue of only the first formula formatting, it creates more issues as it is now adding extra brackets and and u' all over the place. I will edit my question to include a new example of my updated issue. – Michael Rodney Nov 13 '18 at 16:26
  • The u' is likely caused by the output being a unicode object rather than a string. You should be able to remedy that by casting as a string. c = [str(i.strip(',')) for i in a[1:-1].split()] For example. – Matt Needle Nov 13 '18 at 16:33
  • Thanks Matt! That suggestion fixed the u, but I am still having each formula print inside ' ' single quotes, and the total line printing inside []. Do you know how I can fix this? – Michael Rodney Nov 13 '18 at 16:45
  • Your variables e and f are outputting lists, hence the [ ] surrounding them. You could also cast them as strings and then remove the first and last character with string slicing. a = [1,2,3] str(a)[1:-1] #returns '1,2,3' – Matt Needle Nov 13 '18 at 16:53
  • Small update: str(a)[1:-1].replace("'","") will remove the single quotes. It's a little hard to see but in the first argument of replace method there are double quotes surrounding a single quote and the second argument is an empty string (2 double quotes). The replace() will exchange your single quote for nothing. – Matt Needle Nov 13 '18 at 17:02
1

To update 3:

The error traceback indicates a problem in line 17:

c = [str(i.strip(',')) for i in a[1:-1].split()]

Specifically, TypeError: 'NoneType' object has no attribute '__getitem__' . getitem is being called when it attempts to slice string a:

a[1:-1]

and is failing because a is null and since:

a = [2018ExceedanceFigureData-SGPLF_mhuber2.csv.SPRING_EXC]

[2018ExceedanceFigureData-SGPLF_mhuber2.csv.SPRING_EXC] must be null. If the code worked before, you must not have had any null values, but maybe empty strings instead.

  • Thank you for trying to help me understand the errors - I had no idea it wouldn't work on Null Values. - you got it - thanks so much!! I did switch from string to Null values as it was quicker for my data input, but I thought it worked since that point so I didn't consider it a possibility. Thanks again!! – Michael Rodney Jan 24 at 22:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.