ERROR:TypeError:只能加入可迭代对象

时间:2019-05-08 07:18:55

标签: python web-scraping

我需要将数据“ {u'town':u'Bainbridge'}清除为唯一的bainbridge

def clean(self,text):
    if text:
        return ' '.join(''.join(text).split())

ustaApproved = self.clean(raw_ustaApproved)
certifications = self.clean(raw_certifications)

1 个答案:

答案 0 :(得分:0)

从您的帖子中还不清楚,但是我猜想{u'town': u'Bainbridge'}是字符串。您需要将其解析为字典,但首先,我们将其转换为JSON:

import json

def clean(text):
    text = text.replace("u", "")
    text = text.replace("'", '"')
    return json.loads(s)

s = "{u'town': u'Bainbridge'}"
d = clean(s)
desired_value = d["town"]
# => Bainbridge