程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Analysis of problems encountered by python2 in reading Chinese file encoding

編輯:Python

Code :

f = open("1.txt","r")
s = f.read()

View type :

type(s)

The result is str type (byte), There's no mistake here .

Error reporting condition 1 :

s + u'XXX'

str Type plus one utf-8 What's wrong with encoding a string ?

s Will be implicitly converted to utf-8, however s There may be some Chinese characters that are not in utf-8 The coding of , It will lead to an error !

processing method :s.decode("gb2312") Use gb2312 First to unicode There is no problem with splicing types .

Error reporting condition 2 :

sqlite3.connect( Chinese path )

If the Chinese path is str The type will not be found , You need to change the Chinese path to unicode, Still need to use decode("gb2312").


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved