ARTICLE AD BOX
I have a pandas DataFrame that I wish to paste into an HTML document. The DataFrame contains Dingbat characters used as symbols to highlight values as good (checkmark), nearly bad (triangle), or bad (exclamation point). Here are the pertinent code snippets:
df.loc[0, 0] = '\u2705' html = df.to_html(justify='center')I then use the following function to insert the HTML string html into my template HTML file:
def injectHTML(obj_to_insert, dest_file_path, start_string): for line in fileinput.FileInput(dest_file_path, inplace=1): if start_string in line: line = line.replace(line,line+obj_to_insert) print (line, end=" ")Running the whole code, I get the following error:
Traceback (most recent call last): File ~\AppData\Local\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals) File c:\users\m324461\documents\github\my_app\sandbox.py:956 createReport() File c:\users\m324461\documents\github\my_app\sandbox.py:851 in createReport injectHTML(html, foutname, "#_Table") File c:\users\m324461\documents\github\my_app\sandbox.py:151 in injectHTML print (line, end=" ") File ~\AppData\Local\anaconda3\Lib\encodings\cp1252.py:19 in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u2705' in position 723: character maps to <undefined>I tried changing the encoding:
print (line.encode("utf-8", end=" ")This executed without error, but it made the output completely unreadable. I also tried encoding in ASCII, but got the error UnicodeEncodeError: 'ascii' codec can't encode character '\u2705' in position 696: ordinal not in range(128).
I am using Python 3.11.5 and pandas 2.0.3.
What am I doing wrong? Thanks in advance for your help.
