1. 程式人生 > >'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

問題

rds_content = "{}, 執行了變更,sql語句:{}".format(ExecuteTime, sqls)

'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

解決方法:

由於字串中包含中文字元,超出了acsii編碼範圍(128)。
只需要將字串前面加上u字元,將其作為unicode進行處理即可。

rds_content = u"{}執行了變更,sql語句:{}".format(ExecuteTime, sqls)

參考

https://blog.csdn.net/lanyang123456/article/details/84436784

https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20

http://in355hz.iteye.com/blog/1860787