UnicodeEncodeError:'charmap'编解码器无法在位置X-Y处编码字符:字符映射到<undefined>

时间:2018-06-25 23:36:31

标签: python unicode codec

是的,有很多类似的问题,但是对于像这样的特定场景,我没有发现任何问题。 (如果有人真的偶然发现了另一个回答该问题的问题,我将删除该问题。请让我知道。)

click_time = time.strftime('%I:%M:%S %p')
with open('test.txt', 'a') as f:
    #f.write('click: ' + str(x) + ' on %s\n' % click_time)
    #f.write('[ %s ]#' + str(x).zfill(4) + ' to scroll.\n' % click_time)

顶部的f.write即使抛出相同的错误,实际上也会按预期输出,以创建类似这样的文件...

[ 04:29:07 PM ] Created file.       # created elsewhere in the code
click: 1 on 04:29:07 PM
click: 2 on 04:29:07 PM
click: 3 on 04:29:08 PM
click: 4 on 04:29:08 PM
click: 5 on 04:29:08 PM
click: 6 on 04:29:09 PM
click: 7 on 04:29:09 PM
click: 8 on 04:29:09 PM
click: 9 on 04:29:10 PM
click: 10 on 04:29:10 PM
click: 11 on 04:29:10 PM
click: 12 on 04:29:11 PM
click: 13 on 04:29:11 PM
click: 14 on 04:29:11 PM
[ 04:29:11 PM ] Finished scrolling. # created elsewhere in the code

底部的f.write仍会输出,但输出基本上已中断。

[ 04:32:05 PM ] Created file.       # created elsewhere in the code
[ 04:32:05 PM ] Finished scrolling. # created elsewhere in the code

我想知道为什么会出现差异? 为何尽管出现错误,顶部的f.write仍能工作,而底部的f.write却无法产生某些输出?

0 个答案:

没有答案