Код: Выделить всё
df.loc[0, 0] = '\u2705'
html = df.to_html(justify='center')
Код: Выделить всё
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=" ")
Код: Выделить всё
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
Код: Выделить всё
print (line.encode("utf-8", end=" ")
Я использую Python 3.11.5 и pandas 2.0.3.
Что я делаю не так? Заранее спасибо за вашу помощь.
РЕДАКТИРОВАТЬ 20 марта 2026 г. Я изменил функцию инъекции на следующую на основе ответа, который по какой-то причине исчез:
Код: Выделить всё
def injectHTML(obj_to_insert, dest_file_path, start_string):
with open(dest_file_path, "r", encoding="utf-8") as f:
lines = f.readlines()
with open(dest_file_path, "w", encoding="utf-8") as f:
for line in lines:
if start_string in line:
line = line + obj_to_insert
f.write(line)
Подробнее: https://stackoverflow.com/questions/799 ... ng-to-html
Мобильная версия