I am learning python and try to analyze the code performance by line. I installed line_profiler but it always showed the
UnicodeEncodeError: 'charmap' codec can't encode characters in position 30-31: character maps to undefined
I tried to run chcp 65001 which didn't work.
import cgi
import time
from line_profiler import LineProfiler
def test2():
print('hello!test2()')
def test1():
html='''<script>alert("you are a good boy!&I like you")</scrpit>'''
test2()
escape_html=cgi.escape(html)
for item in range(5):
time.sleep(1)
print(escape_html)
if __name__=='__main__':
lp=LineProfiler()
lp.add_function(test2)
lp_wrap=lp(test1)
lp_wrap()
lp.print_stats()
The results:
hello!test2()
C:\OneDrive\u684c\u9762\linecheck.py:91: DeprecationWarning: cgi.escape is deprecated, use html.escape instead escape_html=cgi.escape(html)
<script>alert("you are a good boy!&I like you")</scrpit> Timer unit: 5.14057e-07 s
Total time: 2.6731e-05 s
Traceback (most recent call last):
File "C:\OneDrive\u684c\u9762\linecheck.py", line 101, in
lp.print_stats()
File "C:\Python\lib\site-packages\line_profiler.py", line 139, in print_stats
show_text(lstats.timings, lstats.unit, output_unit=output_unit, stream=stream, stripzeros=stripzeros)
File "C:\Python\lib\site-packages\line_profiler.py", line 261, in show_text
output_unit=output_unit, stream=stream, stripzeros=stripzeros)
File "C:\Python\lib\site-packages\line_profiler.py", line 210, in show_func
stream.write("File: %s\n" % filename)
File "C:\Python\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 30-31: character maps to [Finished in 5.8s]