0

我正在学习python并尝试逐行分析代码性能。我安装了 line_profiler 但它总是显示

UnicodeEncodeError:“charmap”编解码器无法对位置 30-31 中的字符进行编码:字符映射到未定义

我试图运行 chcp 65001 但没有用。

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()

结果:

你好!test2()

C:\OneDrive\u684c\u9762\linecheck.py:91: DeprecationWarning: cgi.escape is deprecated, 使用 html.escape 代替 escape_html=cgi.escape(html)

<script>alert("你是个好孩子!&我喜欢你")</scrpit> 定时器单位:5.14057e-07 s

总时间:2.6731e-05 s

回溯(最近一次通话最后):

文件“C:\OneDrive\u684c\u9762\linecheck.py”,第 101 行,在

lp.print_stats()

文件“C:\Python\lib\site-packages\line_profiler.py”,第 139 行,在 print_stats

show_text(lstats.timings,lstats.unit,output_unit=output_unit,stream=stream,stripzeros=stripzeros)

文件“C:\Python\lib\site-packages\line_profiler.py”,第 261 行,在 show_text

输出单元=输出单元,流=流,stripzeros=stripzeros)

文件“C:\Python\lib\site-packages\line_profiler.py”,第 210 行,在 show_func 中

stream.write("文件: %s\n" % 文件名)

文件“C:\Python\lib\encodings\cp1252.py”,第 19 行,在编码中

返回 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]

4

0 回答 0