post時處理中文字元

2021-06-22 18:58:17 字數 1100 閱讀 8657

做個乙個網頁資訊的查詢,很無奈的發現post資訊中的中文部分錯誤。試了很多方法後發現,把資訊轉換成uft8格式問題解決

name = name.decode('cp936').encode('utf8')

完整**:

# -*- coding: cp936 -*-

import re

import urllib

import urllib2

url = r''

def getmsg(respose):

pat = r'}'

m = re.match(pat,respose)

return m

def showmsg(m):

print '='*20

print '角色名稱 :',m.group(3)

print '角色uid :',m.group(4)

print '角色等級 :',m.group(5)

print '角色超級幣:',m.group(9)

print '角色資金 :',m.group(8)

print '角色vip :',m.group(7)

print '角色榮譽 :',m.group(10)

print '角色經驗 :',m.group(6)

print '角色體力 :',m.group(12)

print '角色聲望 :',m.group(13)

def main():

name = raw_input('輸入角色名稱:')

name = name.decode('cp936').encode('utf8')

value =

data = urllib.urlencode(value)

req = urllib2.request(url,data)

response = urllib2.urlopen(req)

page = response.read()

if (page == ''): print '無法查詢到使用者資訊'

else:showmsg(getmsg(page))

main()

python json中文字元處理

如果json中包含中文字元,我們將其寫入檔案中會怎樣呢?import json data with open data.json w as file file write json.dumps data,indent 2 引數indent指定縮排數。執行之後,結果如下 可以看到結果中,中文字元都變成...

C 中文字元處理

解決方案 構造三層邏輯結構 輸入層 邏輯處理層 輸出層。輸入層接收char輸入,並將其轉換為wchar.邏輯處理層在 wchar 或 wstring 的基礎上進行字串操作,此時操作最小單位為中文字元,不會再有亂碼。輸出層將wchar的結果再次轉換為char 返回給外部。這樣,對外部來說,仍然是輸入c...

處理ANSI中文字元問題

今天寫文字框類,使用ansi編碼對中文處理遇到的問題,現在和大家一起分享一下,如果您覺得這個問題很菜,那你可不並看,也不要,不要出來無理批評.如果您有更深的見解,更好的方式,本人非常願意您能分享.好進入正題,在vc中,使用指標方式儲存字串.如 char str 字串abc123 這個指標長度為 字串...