python - 如何使用不带引号的键从字符串中生成有效的 JSON?

标签 python regex string

我有以下 python 问题:

'{acorn: 15,acorn_type: 15,area_name: "London",beds_max: 1,beds_min: 1,branch_id: "21062",branch_name: "Realm Estates",brand_name: "Realm Estates",chain_free: false,company_id: "10832",country_code: "gb",county_area_name: "London",currency_code: "GBP",display_address: "Lancaster Mews, Wandsworth SW18",furnished_state: "unfurnished",group_id: "",has_epc: true,has_floorplan: false,incode: "1BA",is_retirement_home: false,is_shared_ownership: false,listing_condition: "pre-owned",listing_id: 45296714,listing_status: "to_rent",listings_category: "residential",location: "London",member_type: "agent",num_baths: 1,num_beds: 1,num_images: 5,num_recepts: 1,outcode: "SW18",post_town_name: "London",postal_area: "SW",price: 1300,price_actual: 1300,price_max: 1500,price_min: 1250,price_qualifier: "",property_highlight: "",property_type: "flat",region_name: "London",section: "to-rent",size_sq_feet: "",tenure: "",zindex: "686544"}'

我想要json.load()这个字符串,但显然所有的键都是错误的。它应该是 'acorn: 15,... 等等。我怎样才能将 : 之前的所有单词替换为字符串?谢谢

最佳答案

根据 @daniel-roseman 评论,最好使用 yaml 将字符串加载到 dict 中:

import yaml,json
string='{acorn: 15,acorn_type: 15,area_name: "London",beds_max: 1,beds_min: 1,branch_id: "21062",branch_name: "Realm Estates",brand_name: "Realm Estates",chain_free: false,company_id: "10832",country_code: "gb",county_area_name: "London",currency_code: "GBP",display_address: "Lancaster Mews, Wandsworth SW18",furnished_state: "unfurnished",group_id: "",has_epc: true,has_floorplan: false,incode: "1BA",is_retirement_home: false,is_shared_ownership: false,listing_condition: "pre-owned",listing_id: 45296714,listing_status: "to_rent",listings_category: "residential",location: "London",member_type: "agent",num_baths: 1,num_beds: 1,num_images: 5,num_recepts: 1,outcode: "SW18",post_town_name: "London",postal_area: "SW",price: 1300,price_actual: 1300,price_max: 1500,price_min: 1250,price_qualifier: "",property_highlight: "",property_type: "flat",region_name: "London",section: "to-rent",size_sq_feet: "",tenure: "",zindex: "686544"}'
dict_version = yaml.safe_load(string)
print dict_version

#if you need json version
json_version = json.dumps(dict_version)
print json_version

它给你:

{'listing_status': 'to_rent', 'outcode': 'SW18', 'brand_name': 'Realm Estates', 'member_type': 'agent', 'chain_free': False, 'area_name': 'London', 'is_shared_ownership': False, 'country_code': 'gb', 'beds_max': 1, 'property_type': 'flat', 'incode': '1BA', 'furnished_state': 'unfurnished', 'branch_id': '21062', 'listing_id': 45296714, 'has_floorplan': False, 'beds_min': 1, 'section': 'to-rent', 'company_id': '10832', 'price_min': 1250, 'zindex': '686544', 'location': 'London', 'county_area_name': 'London', 'acorn_type': 15, 'is_retirement_home': False, 'price_max': 1500, 'listing_condition': 'pre-owned', 'display_address': 'Lancaster Mews, Wandsworth SW18', 'region_name': 'London', 'price': 1300, 'num_images': 5, 'listings_category': 'residential', 'branch_name': 'Realm Estates', 'post_town_name': 'London', 'acorn': 15, 'num_recepts': 1, 'size_sq_feet': '', 'price_qualifier': '', 'postal_area': 'SW', 'property_highlight': '', 'price_actual': 1300, 'num_baths': 1, 'num_beds': 1, 'has_epc': True, 'tenure': '', 'group_id': '', 'currency_code': 'GBP'}
{"listing_status": "to_rent", "outcode": "SW18", "brand_name": "Realm Estates", "member_type": "agent", "chain_free": false, "area_name": "London", "is_shared_ownership": false, "country_code": "gb", "beds_max": 1, "property_type": "flat", "incode": "1BA", "furnished_state": "unfurnished", "branch_id": "21062", "listing_id": 45296714, "has_floorplan": false, "beds_min": 1, "section": "to-rent", "company_id": "10832", "price_min": 1250, "zindex": "686544", "location": "London", "county_area_name": "London", "acorn_type": 15, "is_retirement_home": false, "price_max": 1500, "listing_condition": "pre-owned", "display_address": "Lancaster Mews, Wandsworth SW18", "region_name": "London", "price": 1300, "num_images": 5, "listings_category": "residential", "branch_name": "Realm Estates", "post_town_name": "London", "acorn": 15, "num_recepts": 1, "size_sq_feet": "", "price_qualifier": "", "postal_area": "SW", "property_highlight": "", "price_actual": 1300, "num_baths": 1, "num_beds": 1, "has_epc": true, "tenure": "", "group_id": "", "currency_code": "GBP"}

关于python - 如何使用不带引号的键从字符串中生成有效的 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51176980/

相关文章:

python - Blender- python

regex - 如果模式重复两次(非连续)匹配两个模式,正则表达式

c# - 如何使用正则表达式从隐藏字段中获取值属性?

c# - .Net 正则表达式处理中 "$"限定符的潜在不当行为

Java Pattern.matcher(StringBuffer),为什么它的行为与 Pattern.matcher(String) 不同?

c++ - For 循环增量器不能用作字符串 vector 的索引

php - 文件名的字符串清理程序

python - Selenium Chrome 获取文本在 headless 模式下不起作用

python - 使用 dict 参数的具有 OR 条件的 Django 过滤器

python - 使用 numpy 填充输入向量(4 维矩阵)用于卷积神经网络 (CNN)