Am using Python 2.7.6.
Have an HTML file which contains values prepended with "$". Wrote a program which takes in JSON data and replaces the values prepended with $ with the JSON values.
This was working fine until someone opened up the set of HTML files with a different editor and changed it from UTF-8 to ASCII.
class FileUtil:
@staticmethod
def replace_all(output_file, data):
homedir = os.path.expanduser("~")
dest_dir = homedir + "/dest_dir"
with open(output_file, "r") as my_file:
contents = my_file.read()
destination_file = dest_dir + "/" + data["filename"]
fp = open(destination_file, "w")
for key, value in data.iteritems():
contents = contents.replace("$" + str(key), value)
fp.write(contents)
fp.close()
Whenever my program encounters a file which is in ASCII it throws this error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 239, in process
return self.handle()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 420, in _delegate
return handle_class(cls)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 396, in handle_class
return tocall(*args)
FileUtil.replace_all(output_file, data)
File "/home/devuser/demo/utils/fileutils.py", line 11, in replace_all
contents = contents.replace("$" + str(key), value)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 54826: ordinal not in range(128)
Question(s):
Is there a way to make the contents value to be strictly UTF-8 in python?
Is it better to use a command line utility in Ubuntu Linux to convert the file before running this python script?
Is the error an encoding problem (e.g. file is ASCII and not UTF8)?