Hi,
I am trying to develop a python script tool. In this python file I need to use Unicode character like the following:
input_X.replace("°"," "), which intended to replace degree symbol with space, for the purpose of degree minute seconds input. but when I save the python file and reopen again, the file could not recognize the degree symbol and become "?".
Does anyone have a way to solve this problem?
Dave
You are using python 2.7 so put this at the top of your script, you have a unicode issue
# -*- coding: UTF-8 -*- >>> a = "°" >>> a '°' >>> b = "temperature "+a >>> b 'temperature °' >>> c = b.replace(a,"too cold") >>> >>> c 'temperature too cold'
Well this works. If you make the degree symbol in a text file, say "degreetest.txt" and read it in it displays it fine.
import sys, os
# Open the input file *
try:
intemplate = open("C:/data/degreetest.txt", "r")
except IOError:
print "screwup1"
# Read line.
try:
x = intemplate.readline()
except IOError:
print "screwup2"
print x
intemplate.close()
sys.exit("Finto")
also.....
Try this:
import unicodedata
b = unicodedata.lookup('DEGREE SIGN')
print b
and that will get your degree sign. So you can generate it in the script for use.
Paul
hmmmm no need for reading from a file, at least in Canada
if loaded and compiled into a script called Canada.py you will see that it is the utf-8 that is important if using python 2.7. In python 3.x Unicode support is enforced and the concept of a string is a bit fuzzy since everything is generally Unicode or byte.
# -*- coding: UTF-8 -*-
import sys,os
script = os.path.basename(sys.argv[0]).split(".")[0]
a = "°"
print("Ehhh?... a {} of confusion? but not in {}".format(a, script))
I find typing a 'u' to specify a Unicode string helpful:
input_X.replace(u'°',' ')
Randy... prepare for python 3.x, there is no more need to u stuff, u'know'
I've been watching the clock: python 2.7 countdown... Thanks.
posted a new link there just today... I will give you a direct link to it to save you some time
I I tried all of above does not seem to work out, maybe because I am using PyScripter 2.6. Eventually I worked out by the following:
open python file with IDE and add degree symbol to the file when save the IDE prompt to add # -*- coding: cp1252 -*- to the file, click Edit button, the this line was added to the first line and save.
Now it works!!
Thank you everyone in this discussion.
Dave
that is what I suggested way up at the top... whether you do it command line or in a script you need the utf-8 designation, no need for others for a single character.