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

发布于 2022-09-13 00:33:11 字数 2684 浏览 22 评论 0

我想把下面的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}},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷途知返 2022-09-20 00:33:11

"AttrName":".*?"

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

re.findall('"AttrName":"(.*?)"', text, re.IGNORECASE)
画骨成沙 2022-09-20 00:33:11

一定要用正则表达式吗?

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]
且行且努力 2022-09-20 00:33:11
import re
p = re.compile('"attrName":"(.*?)".*?"attrValue":"(.*?)"')
p.findall(text)
唱一曲作罢 2022-09-20 00:33:11

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文