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
dbas
V2EX  ›  Python

python 基础问题请教

  •  1
     
  •   dbas · 2014-12-03 10:18:46 +08:00 · 2621 次点击
    这是一个创建于 3430 天前的主题,其中的信息可能已经有所发展或是发生改变。
    (u'one',{u'a':u"1",u'b':u'2'}),(u'two',{u'ass':u"f1",u'cb':u'2df'})...
    请教我有个str如上,
    如何得到其中的{u'a':u"1",u'b':u'2'} ,{u'ass':u"f1",u'cb':u'2df'}这个字典呢
    10 条回复    2014-12-03 11:31:44 +08:00
    mengzhuo
        1
    mengzhuo  
       2014-12-03 10:34:25 +08:00
    eval()
    不用谢

    跟给你这字符的人说:“还让不让人愉快地干活了!”
    hahastudio
        2
    hahastudio  
       2014-12-03 10:34:32 +08:00
    没太明白= =
    如果你是有一个列表/迭代器或者其他的,打印出来这个的话,有很简单的方法是
    [i[1] for i in lst]

    但如果你是指:
    s = """(u'one',{u'a':u"1",u'b':u'2'}),(u'two',{u'ass':u"f1",u'cb':u'2df'})"""
    我能想到的方法就是 eval 了
    [i[1] for i in eval("[" + s + "]", {'__builtins__':{}})]

    不过,eval 绝对是 Python 里面应该避免使用的函数,看这篇文章
    Eval really is dangerous
    http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html

    直到我发现了 ast.literal_eval
    ast.literal_eval = literal_eval(node_or_string)
    Safely evaluate an expression node or a string containing a Python
    expression. The string or node provided may only consist of the following
    Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
    and None.

    >>> [i[1] for i in ast.literal_eval("[" + s + "]")]
    [{u'a': u'1', u'b': u'2'}, {u'ass': u'f1', u'cb': u'2df'}]
    rrfeng
        3
    rrfeng  
       2014-12-03 10:35:24 +08:00
    str = '''(u'one',{u'a':u"1",u'b':u'2'})'''
    t = eval(str)
    t[1]
    unfurl
        4
    unfurl  
       2014-12-03 10:35:45 +08:00
    用正则在字符串里抠出 {u'a':u"1",u'b':u'2'}
    然后将抠出的字符串传入 eval()方法即可
    1989922yan
        5
    1989922yan  
       2014-12-03 10:39:12 +08:00
    >>> a = ((u'one', {u'a': u'1', u'b': u'2'}), (u'two', {u'ass': u'f1', u'cb': u'2df'}))
    >>> for i in a: print i[1]

    这是你想要的?
    dbas
        6
    dbas  
    OP
       2014-12-03 10:40:55 +08:00
    我的这个是个“tuple”
    dbas
        7
    dbas  
    OP
       2014-12-03 10:41:55 +08:00
    说错了。原来我的这个原型是(u'one',{u'a':u"1",u'b':u'2'}) (u'two',{u'ass':u"f1",u'cb':u'2df'}) 中间没有逗号
    hahastudio
        8
    hahastudio  
       2014-12-03 11:02:27 +08:00
    @dbas 那稍微改改就行啊,意思都一样

    >>> [ast.literal_eval(t) for t in s.split(" ")]
    [(u'one', {u'a': u'1', u'b': u'2'}), (u'two', {u'ass': u'f1', u'cb': u'2df'})]

    如果你还还需要更细致的切字符串的方式的话:
    * 用 re 匹配
    * 修改上游数据的输出格式,这格式谁用谁别扭

    如果你真需要传一个只包含 Python 默认类型的对象的话,为什么不用 cPickle 呢?
    dbas
        9
    dbas  
    OP
       2014-12-03 11:09:41 +08:00
    @hahastudio 没办法这个数据是从接口上得到的。我死的心都想有了
    Muninn
        10
    Muninn  
       2014-12-03 11:31:44 +08:00
    接口里得到的是字符串,用一些json库的方法解析下就搞定了...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5299 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:00 · PVG 14:00 · LAX 23:00 · JFK 02:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.