繁体   English   中英

从python中的String中提取特定单词

[英]Extract specific words from String in python

我想提取“索引”之前的所有单词(例如,ForeverTrophyless,NoPainNoGame,P奖),并将它们全部放入列表中。 我怎样才能做到这一点?

foo = '[{"text":"ForeverTrophyless","indices":[0,18]},{"text":"ForeverTrophyless","indices":[19,37]},{"text":"Prize","indices":[38,56]},{"text":"ForeverTrophyless","indices":[57,75]},{"text":"NoPainNoGame","indices":[76,94]},{"text":"ForeverTrophyless","indices":[95,113]},{"text":"ForeverTrophyless","indices":[114,132]}]'

Python2.7

Pycharm Ubuntu 14.04

您可以使用ast.literal_eval将该字符串转换为词典列表。

foo = '[{"text":"ForeverTrophyless","indices":[0,18]},{"text":"ForeverTrophyless","indices":[19,37]},{"text":"Prize","indices":[38,56]},{"text":"ForeverTrophyless","indices":[57,75]},{"text":"NoPainNoGame","indices":[76,94]},{"text":"ForeverTrophyless","indices":[95,113]},{"text":"ForeverTrophyless","indices":[114,132]}]'

import ast
l = ast.literal_eval(foo)

l现在是:

[{'indices': [0, 18], 'text': 'ForeverTrophyless'},
 {'indices': [19, 37], 'text': 'ForeverTrophyless'},
 {'indices': [38, 56], 'text': 'Prize'},
 {'indices': [57, 75], 'text': 'ForeverTrophyless'},
 {'indices': [76, 94], 'text': 'NoPainNoGame'},
 {'indices': [95, 113], 'text': 'ForeverTrophyless'},
 {'indices': [114, 132], 'text': 'ForeverTrophyless'}]

然后使用列表理解

[i['text'] for i in l]

结果

['ForeverTrophyless', 'ForeverTrophyless', 'Prize', 'ForeverTrophyless', 'NoPainNoGame', 'ForeverTrophyless', 'ForeverTrophyless']

foo似乎是有效的序列化JSON对象。 您可以使用json.loads解析它,然后检索列表json.loads所有text字段:

In [8]: from json import loads

In [9]: [x['text'] for x in loads(foo)]
Out[9]: 
['ForeverTrophyless',
 'ForeverTrophyless',
 'Prize',
 'ForeverTrophyless',
 'NoPainNoGame',
 'ForeverTrophyless',
 'ForeverTrophyless']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM