value = 'window.dataLayer=[{"articleCondition":"New","categoryNr":"12345","sellerCustomerNr":"88888888","articleStatus":"Open"}]'
value = value.split('=')[1]
data = eval(value)[0]
articleCondition = data['articleCondition']
方法2
使用regex
import re
re.findall('"articleCondition":"(\w*)"',value)
使用
json
。您的字符串是:您可以通过拆分获得
=
的右侧:然后用
json
模块解析它:请注意,这是因为在RHS中有双引号。JSON中不允许使用单引号。你知道吗
你有一本词典的目录。使用字典键获取值。你知道吗
例如:
输出:
在python中,有许多方法可以从字符串中获取值,可以使用
regex
、pythoneval
函数,甚至还有更多我可能不知道的方法。你知道吗方法1
方法2
使用
regex
对于
regex
,您可以更具创造性地创建一个通用模式。你知道吗相关问题 更多 >
编程相关推荐