Select to view content in your preferred language

I'M GETTING THIS ERROR "TypeError: cannot concatenate 'str' and 'int' objects"

930
4
Jump to solution
07-02-2013 07:35 AM
OLANIYANOLAKUNLE
Frequent Contributor
I was trying to construct the sql statement below to label a feature

lyr = arcpy.mapping.ListLayers(mxd, "Line_Clip")[0] for lblClass in lyr.labelClasses:     lblClass.SQLQuery = '"ParcelID"' + "=" + str(val) + "AND" + "Bearing" + ">" + 180 TypeError: cannot concatenate 'str' and 'int' objects


Please any help, thank you.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
markdenil
Frequent Contributor
To start, try casting the 180 to a str
str(180)

You might want to add some spaces in the string you are building
val = 100
'"ParcelID"' + "=" + str(val) + "AND" + "Bearing" + ">" + str(180)
yeilds
"ParcelID"=100ANDBearing>180

try
val = 100
'"ParcelID" = ' + str(val) + ' AND "Bearing" > ' + str(180)
which gives you
'"ParcelID" = 100 AND "Bearing" > 180'

View solution in original post

0 Kudos
4 Replies
LindseyWood
Deactivated User
lyr = arcpy.mapping.ListLayers(mxd, "Line_Clip")[0]
for lblClass in lyr.labelClasses:
    lblClass.SQLQuery = '"ParcelID"' + "=" + str(val) + "AND" + "Bearing" + ">" + 180
TypeError: cannot concatenate 'str' and 'int' objects

I believe it is your 180 value its an integer make the whole thing a variable like you would want it to look to sql then pass that to lblCalss.SQLQuery.

query = ' %s = %s AND Bearing > 180' %(ParcleID,val)

http://stackoverflow.com/questions/1225637/python-string-formatting

I like the c formatting way but there are many that would make it easier.

or just str(180)
0 Kudos
markdenil
Frequent Contributor
To start, try casting the 180 to a str
str(180)

You might want to add some spaces in the string you are building
val = 100
'"ParcelID"' + "=" + str(val) + "AND" + "Bearing" + ">" + str(180)
yeilds
"ParcelID"=100ANDBearing>180

try
val = 100
'"ParcelID" = ' + str(val) + ' AND "Bearing" > ' + str(180)
which gives you
'"ParcelID" = 100 AND "Bearing" > 180'
0 Kudos
RhettZufelt
MVP Notable Contributor
This works.

lblClass.SQLQuery = '"ParcelID"' + "=" + str(val) + " AND " + '"Bearing"' + " > " + "180"


R_
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks Y'All mdenil, lmwood & rzufelt you guys are the BEST.....
0 Kudos