當前位置:首頁 » 編程語言 » pythongb2312

pythongb2312

發布時間: 2023-06-10 03:31:19

python怎麼把ASNI(GB2312)轉換成UTF-8

#!/usr/bin/envpython3
fromchardetimportdetect
fn='a.txt'
withopen(fn,'rb')asf:
s=f.read()
coding=detect(s)['encoding']
print('coding:{}'.format(coding))
print('content:{}'.format(s.decode(coding).rstrip()))
newf='b.txt'
withopen(newf,'wb')asf:
f.write(s.decode(coding).encode('utf8'))
print('done!convertcodingtoutf-8andwirtecontentin`{}`'.format(newf))

如果報ImportError那就

pipinstallchardet

def 你理解代碼後自己整合吧

Ⅱ 怎樣優雅解決 python 解析 xml gb2312 編碼的問題

你以二進制形式讀入XML文件,讀入到bytes類型對象中,然後bytes.decode傳編碼參數就能指定二進制數據的編碼、轉換為python內部使用的utf-8。
bytes.decode(encoding="gb2312")

Ⅲ python 將漢字 輸出GB2312

我用的python3.5 用ascii函數處理

s="中文"
lst=[]
forcins:
lst.append(int('0x'+ascii(c)[3:7],16))

print(lst)

輸出是:[20013, 25991]

Ⅳ python3怎樣把gb2312的文件轉換成utf-8格式

str.decode("gb2312").encode("utf-8")
decode
是解碼,把一直的gb2312格式轉換為中間格式unicode,encode再轉換為你需要的utf-8

Ⅳ 用python抓取編碼為gb2312的網頁,結果抓取的都是亂碼 怎樣才能將它弄成正常的HTML格式

你試試下面的代碼

#!/usr/bin/envpython
#-*-coding:utf8-*-

importurllib2

req=urllib2.Request("http://www..com/")
res=urllib2.urlopen(req)
html=res.read()
res.close()

html=unicode(html,"gb2312").encode("utf8")
printhtml

Ⅵ 怎樣優雅解決 python 解析 xml gb2312 編碼的問題

解決辦法:

由於xml協會規定,所有xml解析器均需要支持utf-8和utf-16兩種編碼而不要求別的編碼,所以我估計python提供的xml處理模塊就是不支持gb2312的。而windows下的文件,大部分均為gb2312編碼的,因此處理的時候,就會帶來不方便的地方。

解1:利用UltraEdit等工具,將xml文件轉換成UTF-8的,然後encoding="utf-8"即可

熱點內容
超級訪問陳小春應采兒 發布:2025-05-16 09:43:29 瀏覽:477
緩存視頻合並工具最新版 發布:2025-05-16 09:35:03 瀏覽:194
花雨庭伺服器ip地址和埠 發布:2025-05-16 09:34:58 瀏覽:239
同時修改多台伺服器管理地址工具 發布:2025-05-16 09:20:36 瀏覽:421
什麼配置就能玩地平線 發布:2025-05-16 09:13:46 瀏覽:82
python旋轉圖片 發布:2025-05-16 09:13:40 瀏覽:638
少女前線防檢測腳本 發布:2025-05-16 08:59:07 瀏覽:728
編譯器對系統的依賴 發布:2025-05-16 08:37:29 瀏覽:711
javamap數組 發布:2025-05-16 08:37:28 瀏覽:451
移動光貓如何自行修改密碼 發布:2025-05-16 08:20:15 瀏覽:125