I am trying to create a script that automates the generation of a MXD. It does this by the user copying and pasting several variables, running a definition query and exporting an MXD. I can get the script to work with hard coded values as in the code below:

#vari 1
A = "'A', 'B', 'C', 'D'"

#vari 2
B = "'1', '2', '3', '4'"

#Query 1
Query1 = "Field1 = '17' AND NAME in ("+A+")"
#Query 2
Query2 = "Field1 = '1' AND UNIQUE_ID in ("+B+")"

#get list of layers in the mxd
layers = arcpy.mapping.ListLayers(mxd1)

#for the specified list of layers in the mxd, update the definition queries with the current variables
for layer in layers:
    if layer.name == "layer1":
        layer.definitionQuery = Query1
    if layer.name == "layer2":
        layer.definitionQuery = Query2

#save a copy of the template mxd do the work request folder
mxd1.saveACopy(workFolder + "\\H_" + jobNumber + "_01.mxd")

#clear the lock on the mxd
del mxd1

The problem I have is that the user enters the data as A, B, C, D (copy and pastes from a report) and I need to transform that for the definition query into "'A', 'B', 'C'" (adding the apostrophes). I can do this for one value (A = "'" + A + "'") but struggling to work out how to format with multiple values. I have tried:

#add apostrophes to list
string1 = "'"
AA = [string1 + x + string1 for x in A]
BB = [string1 + x + string1 for x in B]

but that didn't work. Put simply I would like to add extra characters to the elements in the list (A, B, C, D.... into "'A', 'B', 'C',...") I would welcome any pointers on how I can resolve this?

  • You will need to iterate and build the string. "'" + A + "'" should be fine but there are other format options like "'%s'," % A which may be a bit clearer. Start with an empty string then for each of the multiple selected quote and append to the string then remove the trailing comma with strip tutorialspoint.com/python/string_strip.htm or yourString[:-1] (all but the last character in the string) Can you provide an example of how you're getting user input so we can suggest a fix? Commented Mar 23, 2015 at 1:07
  • User input is simply using A = raw_input and the user is copy and pasting in the dialog box Commented Mar 23, 2015 at 1:14
  • raw_input doesn't work in catalog/map. Are you running on command line? Commented Mar 23, 2015 at 2:09
  • Running in PyScripter Commented Mar 23, 2015 at 2:30
  • I have voted to close this as it is a purely python question, and could be answered without any GIS knowledge or information (StackOverflow is better suited for such) -- however, I think Alex's Answer will serve your needs :)
    – Erica
    Commented Mar 23, 2015 at 13:41

1 Answer 1


Why don't you have it written to a list?

my_list = list()
A = 'A'
B = 'B'
C = 'C'


The result of str(my_list) will be ['A', 'B', 'C']. To turn it into the end result that you want, just run tuple on it:

  • That's nice, but in the end the definition query needs to be a string, convert the list to a tuple and it will work fine. Hint: myTup = tuple(myList) turns ['A', 'B', 'C'] to ('A', 'B', 'C'), embed str(myTup) in the string... works great! Commented Mar 23, 2015 at 2:28
  • Ah, I missed that part. I'll edit my response to include that. Commented Mar 23, 2015 at 2:35
  • That's it. Now "Field1 = '17' AND NAME in ("+A+")" becomes "Field1 = '17' AND NAME in " + str(tuple(my_list)), the tuple already has the right parenthesis on it - that's a fluke! +1 for the easy solution. Commented Mar 23, 2015 at 2:50
  • @Alex and Michael. That seems to work as described. How would I handle an unknown number of elements in the list. For example one mxd may have 5 elements in the list and the next one has 12. My apologies for not making that clear in the original question. Commented Mar 23, 2015 at 3:29
  • Use a for loop : for x in range (0, len(A)): my_list.append(A[x]) and then do str(tuple(my_list)). Should work. Commented Mar 23, 2015 at 12:59

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