Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-正确的解析方法_Python_Parsing - Fatal编程技术网

Python-正确的解析方法

Python-正确的解析方法,python,parsing,Python,Parsing,我目前正在编写一个脚本,该脚本将与Youtubes API一起使用。我仍在学习如何使用Python正确解析,但对于这样的东西应该采用什么方法,我有点迷茫 我有这个字符串: [{'term': u'Video Blogging', 'scheme': None, 'label': None}, {'term': u'blogging', 'scheme': None, 'label': None}, {'term': u'stuff', 'scheme': None, 'label': None}

我目前正在编写一个脚本,该脚本将与Youtubes API一起使用。我仍在学习如何使用Python正确解析,但对于这样的东西应该采用什么方法,我有点迷茫

我有这个字符串:

[{'term': u'Video Blogging', 'scheme': None, 'label': None}, {'term': u'blogging', 'scheme': None, 'label': None}, {'term': u'stuff', 'scheme': None, 'label': None}, {'term': u'Videos', 'scheme': None, 'label': None}]
我需要把它交给这个:

Video Blogging, blogging, stuff, Videos
解决这个问题的最佳方法是什么?感谢您的帮助,谢谢

>>> l = [{'term': u'Video Blogging', 'scheme': None, 'label': None}, {'term': u'blogging', 'scheme': None, 'label': None}, {'term': u'stuff', 'scheme': None, 'label': None}, {'term': u'Videos', 'scheme': None, 'label': None}]
>>> [a.get('term') for a in l]
[u'Video Blogging', u'blogging', u'stuff', u'Videos']
如果要以逗号分隔的字符串形式获取项目,请使用:

>>> ', '.join(a.get('term') for a in l)
u'Video Blogging, blogging, stuff, Videos'
我使用
a.get('term')
而不是
a['term']
来避免没有
term
键的项目出现
keyrear

如果要以逗号分隔的字符串形式获取项目,请使用:

>>> ', '.join(a.get('term') for a in l)
u'Video Blogging, blogging, stuff, Videos'

我使用了
a.get('term')
而不是
a['term']
来避免对没有
term
键的项目出现
键错误

>>> l = [{'term': u'Video Blogging', 'scheme': None, 'label': None}, {'term': u'blogging', 'scheme': None, 'label': None}, {'term': u'stuff', 'scheme': None, 'label': None}, {'term': u'Videos', 'scheme': None, 'label': None}]
>>> ', '.join([d['term'] for d in l])
u'Video Blogging, blogging, stuff, Videos'

应该是这样的:

>>> l = [{'term': u'Video Blogging', 'scheme': None, 'label': None}, {'term': u'blogging', 'scheme': None, 'label': None}, {'term': u'stuff', 'scheme': None, 'label': None}, {'term': u'Videos', 'scheme': None, 'label': None}]
>>> ', '.join([d['term'] for d in l])
u'Video Blogging, blogging, stuff, Videos'

对于此特定情况,您可以使用列表:

    list_of_stuff = [
            {'term': u'Video Blogging', 'scheme': None, 'label': None}, 
            {'term': u'blogging', 'scheme': None, 'label': None}, 
            {'term': u'stuff', 'scheme': None, 'label': None}, 
            {'term': u'Videos', 'scheme': None, 'label': None}
            ]     

    parsed_string = ', '.join([d['term'] for d in list_of_stuff])

对于此特定情况,您可以使用列表:

    list_of_stuff = [
            {'term': u'Video Blogging', 'scheme': None, 'label': None}, 
            {'term': u'blogging', 'scheme': None, 'label': None}, 
            {'term': u'stuff', 'scheme': None, 'label': None}, 
            {'term': u'Videos', 'scheme': None, 'label': None}
            ]     

    parsed_string = ', '.join([d['term'] for d in list_of_stuff])

你是否在问如何从字典列表中获取所有“术语”值?是的。如果可能的话,总是可能的。有人在我正要回答的时候发布了答案:-)你是在问如何从你的字典列表中获得所有的“术语”值吗?是的。如果可能的话,总是可能的。有人刚刚在我正要回答的时候发布了答案:-)太好了!我从来没有把字典弄对过。谢谢你!完美的我从来没有把字典弄对过。谢谢你!我将把join()调用传递给生成器,而不是列表。不确定微优化weenies会说什么,但反汇编只有长度的一半。我将把join()调用传递给生成器,而不是列表。不知道微优化weenies会说什么,但拆卸的长度只有一半。