AttributeError:“ str”对象没有属性“ map”

时间:2019-03-15 13:17:27

标签: python

我是python的新手。我想从json下面提取数组“地址”的元素。我正在尝试使用map使用

来拆分元素
r=<below json>
s=r["Customer"]
y=s.map(lambda x:x.split(",")) 

但是我得到的错误是..... AttributeError:'str'对象没有属性'map'

您能告诉我哪种方法是最好的方法。

{ "id": "981",
  "Customer": 
[
  {
    "Name": "abc",
    "Title": "Mr",
    "Gender": "M",
    "Address": [
      {
    "Postcode": "2GF",
    "StreetName": "Black Street",
    "FlatNo": "123",
      }
    ]
  },
  {
    "Name": "xyz",
    "Title": "Mrs",
    "Gender": "F",
    "Address": [
      {
    "Postcode": "2CX",
    "StreetName": "White Street",
    "FlatNo": "123",
      }
    ]
  }
]
}

2 个答案:

答案 0 :(得分:0)

“我要从json下面提取数组“地址”的元素”:

[x for dd in r['Customer'] for x in dd['Address']]

答案 1 :(得分:0)

为什么不只是:

data = { id: 981,
  'Customer':
[
  {
    "Name": "abc",
    "Title": "Mr",
    "Gender": "M",
    "Address": [
      {
    "Postcode": "2GF",
    "StreetName": "Black Street",
    "FlatNo": "123",
      }
    ]
  },
  {
    "Name": "xyz",
    "Title": "Mrs",
    "Gender": "F",
    "Address": [
      {
    "Postcode": "2CX",
    "StreetName": "White Street",
    "FlatNo": "123",
      }
    ]
  }
]
}


for item in data['Customer']:
    for data_item in item['Address']:
        print(data_item['Postcode'], data_item['StreetName'], data_item['FlatNo'])

输出

2GF Black Street 123
2CX White Street 123