日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 unicode字符串_【已解决】Python3中如何声明字符串是unicode类型以避免log日志打印出错...

發布時間:2023/12/10 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 unicode字符串_【已解决】Python3中如何声明字符串是unicode类型以避免log日志打印出错... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python3中代碼:

CreateTableSqlTemplate = """CREATE TABLE IF NOT EXISTS `%s` (

`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘自增,主鍵’,

`cityDealerPrice` int(11) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘經銷商參考價’,

`msrpPrice` int(11) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘廠商指導價’,

`mainBrand` char(20) NOT NULL DEFAULT ” COMMENT ‘品牌’,

`subBrand` varchar(20) NOT NULL DEFAULT ” COMMENT ‘子品牌’,

`brandSerie` varchar(20) NOT NULL DEFAULT ” COMMENT ‘車系’,

`brandSerieId` varchar(15) NOT NULL DEFAULT ” COMMENT ‘車系ID’,

`model` varchar(50) NOT NULL DEFAULT ” COMMENT ‘車型’,

`modelId` varchar(15) NOT NULL DEFAULT ” COMMENT ‘車型ID’,

`modelStatus` char(5) NOT NULL DEFAULT ” COMMENT ‘車型狀態’,

`url` varchar(200) NOT NULL DEFAULT ” COMMENT ‘車型url’,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;"""

logging.info("config=%s, needCreateTable=%s, tableName=%s, createTableSqlTemplate=%s",

config, needCreateTable, tableName, createTableSqlTemplate)

結果出錯:

Traceback (most recent call last):

File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 994, in emit

stream.write(msg)

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 334-338: ordinal not in range(128)

然后

試了試:

logging.info("createTableSqlTemplate=%s", createTableSqlTemplate.encode("utf-8"))

結果:

createTableSqlTemplate=b"CREATE TABLE IF NOT EXISTS `%s` (….

輸出了bytes,是不會出錯,但是輸出到都是\xxxx,不方便查看原始內容了。

然后也試了試加u前綴:

CreateTableSqlTemplate = u"""CREATE TABLE IF NOT EXISTS `%s` (

`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘自增,主鍵’。。。。"""

問題依舊。

所以想要搞清楚Python3中,如何聲明是unicode字符串

python 3 unicode string

python 3 declare unicode string

試試:

logging.info("createTableSqlTemplate=%s", str(createTableSqlTemplate))

結果問題依舊。

試試:

CreateTableSqlTemplate = b”""xxx""".decode("utf-8")

結果:

SyntaxError: bytes can only contain ASCII literal characters.

試試:

CreateTableSqlTemplate =?“""xxx""".encode("utf-8").decode("utf-8")

結果:

問題類似:

Traceback (most recent call last):

File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 994, in emit

stream.write(msg)

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 152-156: ordinal not in range(128)

貌似出錯的position位置變了?

python 3??UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position??ordinal not in range(128)

此處Python文件最開始已經指明文件編碼為utf-8了:

#!/usr/bin/python

# -*- coding: utf-8 -*-

且文件本身的確是utf-8編碼:

要用到PYTHONIOENCODING?

感覺不太對

試試:

import sys

import io

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding=’utf-8′)

結果問題依舊。

試試:

CreateTableSqlTemplate = str(“""xxx""")

結果:

問題依舊。

此處,好像是這個中文的逗號:

導致出錯的。

-》當然可以直接刪除掉,但是不是好的做法。

還是希望此處可以正常輸出這個逗號的。

去給PyCharm的debug加上:

PYTHONIOENCODING=utf-8

試試

結果:

沒法允許。去加上環境變量中:

問題依舊。

去給filehandler中加上編碼

logging.basicConfig(

level????= fileLogLevel,

format???= fileLogFormat,

datefmt??= fileLogDateFormat,

filename = logFilename,

encoding = "utf-8",

filemode = ‘w’)

結果:

ValueError: Unrecognised argument(s): encoding

python 3 logging.basicConfig encoding

沒有提到encoding或encode

說是不要用basicConfig,換成logging.FileHandler,自己設置文件編碼

然后試試自己使用fileHandler

rootLogger = logging.getLogger()

rootLogger.setLevel(fileLogLevel)

fileHandler = logging.FileHandler(

filename=logFilename,

mode=’w’,

encoding="utf-8")

fileHandler.setFormatter = logging.Formatter(

fmt=fileLogFormat,

datefmt=fileLogDateFormat

)

rootLogger.addHandler(fileHandler)

結果:

就可以正常打印log了:

【總結】

此處Python3中,對于定義好了的一個字符串:

someStr = """xxx"""

其中xxx中包含了一個中文的逗號,然后去logging去打印日志,然后出錯:

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 334-338: ordinal not in range(128)

最后確定根本原因是:

初始化logging時,用的是basicConfig,不支持指定文件編碼

導致默認fileHandler的文件編碼(估計)是ASCII,然后無法輸出此處中文字符

解決辦法是:

設置logging的fileHandler的(文件的)encoding

具體做法:

rootLogger = logging.getLogger()

rootLogger.setLevel(fileLogLevel)

fileHandler = logging.FileHandler(

filename=logFilename,

mode=’w’,

encoding="utf-8")

fileHandler.setFormatter = logging.Formatter(

fmt=fileLogFormat,

datefmt=fileLogDateFormat

)

rootLogger.addHandler(fileHandler)

然后即可正常輸出日志。

總結

以上是生活随笔為你收集整理的python3 unicode字符串_【已解决】Python3中如何声明字符串是unicode类型以避免log日志打印出错...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。