Field values returned in unicode

2512
7
Jump to solution
07-16-2014 06:19 AM
Zeke
by
Regular Contributor III

In the code below, the field values are returned in unicode (e.g. (u'  06510',)). How do I get this to return ascii, as in 06510,? Thanks. Hopefully doing this right, new forums confusing.

with arcpy.da.SearchCursor(fc, "OLDPLATE") as rows:

        for row in rows:

            if row is None or len(row) == 0: lstNullOldPlates.append("Null or empty")

            else: lstAllOldPlates.append('{0}'.format(row))

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Zeke
by
Regular Contributor III

Thanks Ian, but ord didn't work. The values are test, a mix of letters, blanks and numbers. Ord() works on one at a time, and gave me a TypeError when I tried parsing the values.

I did manage to fix the problem though. I'd forgotten that arcpy.da returns a tuple, not the (in this case) text value. When I added row[0] to the processing instead of just row, it worked as desired. Thanks everyone. I hesitate to mark my own answer as correct, don't want to violate protocol, but on the other hand that might be useful to others.

View solution in original post

0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Greg,

  You should always add a tag to your post in the new forums (i.e. Python) so that the python people can find your discussion and better yet start your discussion in the python space Python

Robert

0 Kudos
IanMurray
Frequent Contributor

you need to call the ord() method on it and it should convert your unicode string to ascii.

2. Built-in Functions — Python v2.7.8 documentation

0 Kudos
DonOpgenorth
Esri Contributor

I've
moved your post into the
Python  space. You will get a much
better answer here as the
GeoNet Help is intended for community
help and feedback. I have also added in a few additional tags. You can see more
on the community structure, and what topics are under each space from the
following documents:

GeoNet Community Structure

ArcGIS Discussion Forums Migration Strategy

Thanks!

Don

Zeke
by
Regular Contributor III

Thanks all. I searched for a python oriented forum, couldn't find it. This new format will really take some getting used to.

0 Kudos
Zeke
by
Regular Contributor III

Thanks Ian, but ord didn't work. The values are test, a mix of letters, blanks and numbers. Ord() works on one at a time, and gave me a TypeError when I tried parsing the values.

I did manage to fix the problem though. I'd forgotten that arcpy.da returns a tuple, not the (in this case) text value. When I added row[0] to the processing instead of just row, it worked as desired. Thanks everyone. I hesitate to mark my own answer as correct, don't want to violate protocol, but on the other hand that might be useful to others.

0 Kudos
IanMurray
Frequent Contributor

Ah, well I'm glad you got it worked out. 

0 Kudos
curtvprice
MVP Esteemed Contributor

What is your use case that requires you to convert unicode? Normal ascii characters should always be treated the same whether they are unicode or not.

Python treats unicode and text pretty much the same if the content doesn't contain any international characters. In Python 3.4 there is no distinction. The only way you can tell it is unicode is when it is rendered by repr(), for example when you enter the variable at the command line.

0 Kudos