python - 关于字符编码问题
大家讲道理
大家讲道理 2017-04-18 10:03:44
0
6
570
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(6)
Peter_Zhu
data = {
    "title": "魅族 32G 魅蓝note5"
}
import json
print(json.dumps(data, ensure_ascii=False, encoding="utf-8"))
小葫芦
chcp 65001

Just type it directly in the terminal.

ლ(╹◡╹ლ), um, maybe, huh?

巴扎黑

.encode('utf-8') try it

PHPzhong

With python3, you don’t have these worries anymore

左手右手慢动作
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 26 2016, 12:10:39)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> import requests
>>> data = requests.get('http://zhuanzhuan.58.com/detail/807158202296418308z.shtml').content
>>> soup = BeautifulSoup(data,'lxml')
>>> title = soup.select('.box_left_top h1')[0].text.encode('utf-8')
>>> print type(title)
<type 'str'>
>>> print title
魅族  32G 魅蓝note5
>>> test_data = {'title':title}
>>> print test_data
{'title': '\xe9\xad\x85\xe6\x97\x8f  32G \xe9\xad\x85\xe8\x93\x9dnote5'}
>>> print repr(test_data).decode("unicode-escape")
{'title': u'魅族  32G 魅蓝note5'}
黄舟

The method works, thank you

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template