I am using Python to read a text file of data line by line. One of the lines contains a degree symbol. I want to alter this part of the string. My script uses line = line.replace("TEMP [°C]", "TempC")
. My code stops at this line but does not change the sting at all nor does it throw an error. Clearly there is something about my replace such that the script does not see the 'TEMP [°C]' as existing in my string.
In order to insert the degree sign in my script I had to change the encoding to UTF-8 in my IDE file settings. I have included the following text at the top of my script.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
How do I replace 'TEMP [°C]' with 'TempC'?
I am using Windows 7 and Python 2.7 with Komodo IDE 5.2
I have tried running the suggested code in a Python Shell in Komodo and that changed the file.
# -*- coding: utf-8 -*-
line = "hello TEMP [°C]"
line = line.replace("TEMP [°C]", "TempC")
print(line)
hello TempC
This suggested code in a Python Shell in Komodo returned this.
line = "TEMP [°C]"
line = line.replace(u"TEMP [°C]", "TempC")
Traceback (most recent call last):
File "<console>", line 0, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 6: ordinal not in range(128)
None of these suggestions worked when reading my text file though.