簡體   English   中英

python日志記錄,打開記事本無法理解內容

[英]python logging,open notepad can't understand the content

我使用以下代碼記錄日志,其中有英文,日文和中文。“ aaa”是英文。“くれ対応​​”是日文。“我啊”是中文。

import logging
import datetime
import os

logger = logging.getLogger()
now_date = datetime.datetime.now().strftime('%Y%m%d')
log_file = now_date+".log"
if not os.path.exists("log"):
    os.makedirs("log")
formatter = logging.Formatter('%(asctime)s %(levelname)s line:%(lineno)s %(message)s')
file_handler = logging.FileHandler("log"+os.sep+log_file)
file_handler.setFormatter(formatter) 
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)
logger.error('aaaくれ対応我啊'.encode('utf-8'))

當我打開記事本時,內容是這樣的:

2018-03-20 10:28:46,451 ERROR line:2161 b'aaa\xe3\x81\x8f\xe3\x82\x8c\xe5\xaf\xbe\xe5\xbf\x9c\xe6\x88\x91\xe5\x95\x8a'

我無法理解b'aaa \\ xe3 \\ x81 \\ x8f \\ xe3 \\ x82 \\ x8c \\ xe5 \\ xaf \\ xbe \\ xe5 \\ xbf \\ x9c \\ xe6 \\ x88 \\ x91 \\ xe5 \\ x95 \\ x8a'。 打開記事本時如何將其轉換為中文和日文?

您只需要在文件處理程序中配置編碼,而不是自己編碼

file_handler = logging.FileHandler("log"+os.sep+log_file,'w', 'utf-8')

刪除最后一行中的編碼

logger.error('aaaくれ対応我啊')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM