如果用python正则把这个内容提取出来。

如果用python正则把这个内容提取出来。

我想把下面的attrName和attrValue的值提取出来,写了好几种模式一直匹配不出来,请问要怎么写呢?
谢谢

[{"attrName":"Place of Origin","attrNameId":1,"attrValue":"China"},{"attrName":"Brand Name","attrNameId":2,"attrValue":"ZHONGKAI"},{"attrName":"Model Number","attrNameId":3,"attrValue":"45CC"},{"attrName":"Industrial Use","attrNameId":19421,"attrValue":"gift packaging","attrValueId":394282130},{"attrName":"Use","attrNameId":19427,"attrValue":"gift packaging","attrValueId":394282130},{"attrName":"Paper Type","attrNameId":19445,"attrValue":"cardboard","attrValueId":4},{"attrName":"Printing Handling","attrNameId":100003805,"attrValue":"Matt Lamination","attrValueId":1875805820},{"attrName":"Custom Order","attrNameId":100005412,"attrValue":"Accept","attrValueId":6722354},{"attrName":"Feature","attrNameId":191284141,"attrValue":"Disposable","attrValueId":76328288},{"attrName":"Shape","attrNameId":230093740,"attrValue":"square, square"},{"attrName":"Product name","attrValue":"Luxury Black Marble Jewelry Gift Box With Silver Foiling Logo"},{"attrName":"Usage","attrValue":"Jewelry Packaging"},{"attrName":"Size","attrValue":"Follow Customer's Requirement"},{"attrName":"Delivery time","attrValue":"About 8 workdays,based on QTY"},{"attrName":"Logo","attrValue":"Customer's Logo"},{"attrName":"Printing","attrValue":"CMYK or Pantone"},{"attrName":"Color","attrValue":"Black, Accept Customized"},{"attrName":"Shipping way","attrValue":"Express, sea transportation, air transport.etc"},{"attrName":"OEM/ODM","attrValue":"Avaliable"}],"productCategoryId":201268096,"productEncryptId":"IDX1x7H_-HW7-UJbBw-xc7vERtCg_xN1GTlExetkleQFEv-yvG333gL-3KRsKLDkssa3","productId":62437805348,"productIsCertified":false,"productIsExhibition":false,"productIsHang":false,"productIsMarketGoods":false,"productIsPersonal":false,"productLightCustomizationList":[{"customType":"Customized logo","moq":500},{"customType":"Customized packaging","moq":500},{"customType":"Graphic customization","moq":500},{"customType":"Custom Material","moq":500},{"customType":"Custom Size","moq":500}],"sample":{"priceUnit":"Piece","productSamplingOrderQuantity":2,"productSamplingType":"orderSample"},"sku":{"skuAttrs":[{"id":200001176,"name":"Color","values":[{"color":"#000000","fileName":"","id":3327837,"name":"Black","selected":true,"type":"COLOR","usable":true}]},{"id":200009677,"name":"Size","values":[{"color":"","fileName":"","id":-1,"name":"no","selected":true,"type":"TEXT","usable":true}]},{"id":208996422,"name":"Thickness","values":[{"color":"","fileName":"","id":-2,"name":"no","selected":true,"type":"TEXT","usable":true}]}],"skuInfoMap":{"200001176:3327837;200009677:-1;208996422:-2;":{"id":100611879599}},


回答:

"AttrName":".*?"

.*? 非贪婪匹配,确保能匹配出双引号里的内容,你可以加个括号来提取匹配组"AttrName":"(.*?)",方便在 python 里用 re.findall 提取出来。

python3">re.findall('"AttrName":"(.*?)"', text, re.IGNORECASE)


回答:

一定要用正则表达式吗?

import json

text=... #贴的text不全,应该是合法的json吧

d=json.loads(text)

attrNames=[x.get("attrName") for x in d]

attrValues=[x.get("attrValue") for x in d]


回答:

import re

p = re.compile('"attrName":"(.*?)".*?"attrValue":"(.*?)"')

p.findall(text)


回答:

感觉上是你的数据不完整,本来应该是比较好的json的,用json解析会方便很多的。

以上是 如果用python正则把这个内容提取出来。 的全部内容, 来源链接: utcz.com/p/938020.html

回到顶部