Python - Remove quotation marks from combobox.

1497
5
Jump to solution
05-07-2019 10:02 AM
DaleShearer
Occasional Contributor

Thanks for looking at my question.

       This is my first time working with Python so I am on the learning curve.  I am creating a menu and I am populating a combo box from a field in a feature class in a file geodatabase.  It works thought the data retrieved needs cleaned up, I have tried a few different way to retrieve the data and I get the same results.

I want to populate the combo box with map numbers, which I do get....  I want to see the map numbers looking like 01-01-01, etc.  I get [[(u'01-01-01',),.  I realize the [[ brackets are because I am putting the data is a list.  Not sure on the rest of it.

The following is my code:

from tkinter import ttk

cursor = arcpy.da.SearchCursor("R:\Projects\MAP_Projects.gdb\MAP_INDEX","Map_No")
list1 = []
for row in cursor:
list1.append(row)

list1.sort()

comboMapBox = ttk.Combobox(root, width=15, values = [list1])
comboMapBox.grid(column=0, row=1)
comboMapBox.current(0)
comboMapBox.place(x=125,y=60)

Thank you for taking a look, I am pleased I at least have it working....somewhat.  I am just not sure what I am missing, or have not found yet in my searching of the internet.

Dale,

0 Kudos
1 Solution

Accepted Solutions
DaleShearer
Occasional Contributor

Well, that was a long trip from here to there....

The 'u' signifies unicode, learned that....

So in the spirit of just keep digging I came across where I need to return the ascii version of the string. 

Added:  [0].encode("utf-8") to the append statement.

Was:  list1.append(row)

Now:  list1.append(row[0].encode("utf-8"))

So:

for row in cursor:  
     list1.append(row[0].encode("utf-8"))

I now have the format of 01-01-01 in my combo box instead of  (u'01-01-01',).

I hope this helps someone else out.

View solution in original post

0 Kudos
5 Replies
RandyBurton
MVP Alum

The search cursor returns a tuple.  Try taking the first element in the tuple:

list1 = [] 
cursor = arcpy.da.SearchCursor("R:\Projects\MAP_Projects.gdb\MAP_INDEX","Map_No")
    for row in cursor:
        list1.append(row[0]) # take first element in row

list1.sort()‍‍‍‍‍‍‍‍‍‍
0 Kudos
DaleShearer
Occasional Contributor

Thanks for the reply, though I still got the same result.  I did more searching on the internet in relation to removing the brackets and quotation marks though the results are more applicable to removing characters from a string, not from a combo box.

So still searching, you think it would be a simple process.

0 Kudos
DaleShearer
Occasional Contributor

Well, I found something just by trying different things.

If I do the following I get the map number in the format I want, 01-01-01, but only 1 map number, not all the map numbers.

The following is my code:

 

from tkinter import ttk

 

cursor = arcpy.da.SearchCursor("R:\Projects\MAP_Projects.gdb\MAP_INDEX","Map_No")
list1 = []
for row in cursor:

comboMapBox['values'] = row

 

comboMapBox = ttk.Combobox(root, width=15)
comboMapBox.grid(column=0, row=1)
comboMapBox.current(0)
comboMapBox.place(x=125,y=60)

I know I am only setting the value to the last read value, so????

0 Kudos
DaleShearer
Occasional Contributor

Okay, the problem is not a combo box problem, but how the data is retrieved.  When I loop through the cursor (for row in cursor) and do print row each record is formatted the same, number is different: (u'01-01-01',)

So I need to figure out how remove the: ( u ' , ) from the number.  It is a text field, but I have not been able to strip it as it loops through the cursor and appends it to the list.

0 Kudos
DaleShearer
Occasional Contributor

Well, that was a long trip from here to there....

The 'u' signifies unicode, learned that....

So in the spirit of just keep digging I came across where I need to return the ascii version of the string. 

Added:  [0].encode("utf-8") to the append statement.

Was:  list1.append(row)

Now:  list1.append(row[0].encode("utf-8"))

So:

for row in cursor:  
     list1.append(row[0].encode("utf-8"))

I now have the format of 01-01-01 in my combo box instead of  (u'01-01-01',).

I hope this helps someone else out.

0 Kudos