當前位置:首頁 » 編程語言 » 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-07-03 10:46:38 瀏覽:517
神州防火牆web怎麼配置代碼 發布:2025-07-03 10:37:54 瀏覽:327
安卓看小說哪個軟體免費又最好 發布:2025-07-03 10:25:30 瀏覽:429
linuxprofile 發布:2025-07-03 10:25:29 瀏覽:718
存儲藍盤 發布:2025-07-03 09:55:10 瀏覽:886
java必學 發布:2025-07-03 09:21:57 瀏覽:446
go在線編譯 發布:2025-07-03 09:14:51 瀏覽:19
存儲控制器hp 發布:2025-07-03 08:59:51 瀏覽:504
密碼鎖延安多少一條 發布:2025-07-03 08:58:14 瀏覽:322
linux中gcc 發布:2025-07-03 08:51:04 瀏覽:412