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))
Solved! Go to Solution.
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.
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
you need to call the ord() method on it and it should convert your unicode string to ascii.
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:
ArcGIS Discussion Forums Migration Strategy
Thanks!
Don
Thanks all. I searched for a python oriented forum, couldn't find it. This new format will really take some getting used to.
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.
Ah, well I'm glad you got it worked out.
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.