V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
btwo
V2EX  ›  Python

请教:关于 Python处理JSON的几个小问题

  •  
  •   btwo · 2012-06-29 16:14:41 +08:00 · 4682 次点击
    这是一个创建于 4318 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1.鞋 凉鞋鱼嘴鞋 这样的字符怎么转换成中文?目前没有解决

    2.http:\/\/a.wcmo.cn\/sys\/common\/icon\/rank_s\/b_blue_1.gif 这样的转义符怎么去掉,我现在是一个一个替换的,效率太低了。

    3.\u8d85\u8bbe\u8ba1\u611f\u539a\u5e95\u51c9\u978b 这样的字符怎么转换为中文?现在我用了一个编码转换的函数,效率挺低的。

    我现在是采集几个网站的JSON数据入库到MYSQL的数据库,有同学遇到同样的问题吗?有好的解决办法吗?
    8 条回复    1970-01-01 08:00:00 +08:00
    Livid
        1
    Livid  
    MOD
       2012-06-29 16:20:02 +08:00
    你最好能够把代码和 JSON 都贴出来,这样我们可以更好地帮你。
    btwo
        2
    btwo  
    OP
       2012-06-29 16:21:19 +08:00
    1. &# 38795; &# 20937;&# 38795;&# 40060;&# 22068;&# 38795 的字符, 我提交就自动转换成中文了!汗!
    btwo
        3
    btwo  
    OP
       2012-06-29 16:27:00 +08:00
    1.
    http://love.taobao.com/guang/async_search.htm?t=1340953549726&page=6&tagid=170&qc=&order=4&cat=&price=&version=91&nid=&start_price=&end_price=&market_id=0&s_type=&color= 这个淘宝的一个JSON请求地址

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    import requests
    r = requests.get('http://love.taobao.com/guang/async_search.htm?t=1340953549726&page=6&tagid=170&qc=&order=4&cat=&price=&version=91&nid=&start_price=&end_price=&market_id=0&s_type=&color=')
    print r.text

    由于没解决问题,只测试了下。


    2.
    str= s.replace(r'\r\n', '\n').replace(r'\"', '"').replace(r'\/', '/').replace(r'\t', ' ') 我现在是这样替换的,感觉处理不完美,肯定有更好的解决办法。

    3.
    def jiema(getstring):
    fstr=r""
    list_str=getstring.split(r'\u')
    for single in list_str:
    if single != '':
    fstr+=unichr(int(single,16))
    print fstr

    解码的方法

    JSON的地址:http://www.meilishuo.com//aj/getGoods/catalog?frame=0&page=0&view=1&word=0&cata_id=6000000000000&section=hot&price=all&_=1338983940302
    cute
        4
    cute  
       2012-06-29 16:28:17 +08:00
    import json
    print json.dumps({'name':'中文', 'url':'http://www.v2ex.com/t/40916'}, ensure_ascii=False)
    btwo
        5
    btwo  
    OP
       2012-06-29 16:31:54 +08:00
    补充: 我用得是WINDOWS的环境,是不是和本地编码也有关系,网上找了些资料看,都是几个编码的问题,新手看得有点晕乎了!
    cloudzhou
        6
    cloudzhou  
       2012-06-29 16:54:17 +08:00
    @Livid 这个地方我也有纠结的地方,比如 json.dumps({'xss': '<script>alert(1)</script>'}),如果避免xss呢,尤其是数据的来源有很多种方式,目前我通过人工做的,应该可以通过json的一些机制,统一设置一些处理规则
    cute
        7
    cute  
       2012-06-29 18:31:20 +08:00   ❤️ 1
    @cloudzhou

    import json
    import cgi
    import functools

    def encoder(o, _encoder=json.encoder.encode_basestring):
    .... if isinstance(o, basestring):
    ........o = cgi.escape(o)
    ....return _encoder(o)

    json.encoder.encode_basestring = encoder
    json.encoder.encode_basestring_ascii = functools.partial(encoder, _encoder=json.encoder.encode_basestring_ascii)

    print json.dumps({'<xss>': '<script>alert(1)</script>'})

    {"&lt;xss&gt;": "&lt;script&gt;alert(1)&lt;/script&gt;"}
    cute
        8
    cute  
       2012-06-29 18:32:00 +08:00
    '\u8d85\u8bbe\u8ba1\u611f\u539a\u5e95\u51c9\u978b'.decode('raw_unicode_escape')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5543 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:51 · PVG 10:51 · LAX 19:51 · JFK 22:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.