简体   繁体   English

将 HTML 换行转换为 python 字典

[英]Convert HTML line break into python dictionary

I am scraping incident data from the RFS (Rural Fire Service) website to do some data analyse, However part of the data I get back i need to break it up to be more granular.我正在从 RFS(农村消防局)网站上抓取事件数据以进行一些数据分析,但是我返回的部分数据我需要将其分解为更细化。

I have collected the following straight from a JSON and would appreciate some suggestions on how i could turn this 'description': 'ALERT LEVEL: Not Applicable <br />LOCATION: Turnbulls Lane, Moruya, NSW 2537 <br />COUNCIL AREA: Eurobodalla <br />STATUS: Out of control <br />TYPE: Structure Fire <br />FIRE: Yes <br />SIZE: 0 ha <br />RESPONSIBLE AGENCY: Fire and Rescue NSW <br />UPDATED: 3 Apr 2020 21:14'我直接从 JSON 收集了以下内容,并希望对我如何转换此'description': 'ALERT LEVEL: Not Applicable <br />LOCATION: Turnbulls Lane, Moruya, NSW 2537 <br />COUNCIL AREA: Eurobodalla <br />STATUS: Out of control <br />TYPE: Structure Fire <br />FIRE: Yes <br />SIZE: 0 ha <br />RESPONSIBLE AGENCY: Fire and Rescue NSW <br />UPDATED: 3 Apr 2020 21:14'

into this进入这个

{'ALERT LEVEL': 'Not Applicable', 'LOCATION': 'Turnbulls Lane, Moruya, NSW 2537', 'COUNCIL AREA': 'Eurobodalla', 'STATUS': 'Out of control', 'TYPE': 'Structure Fire', 'FIRE': 'Yes', 'SIZE': '0 ha', 'RESPONSIBLE AGENCY ': 'Fire an
d Rescue NSW', 'UPDATED': '3 Apr 2020 21:14'}

Thanks谢谢

Assuming that the data you retrieved is in Python JSON or otherwise dict-like format, try假设您检索到的数据采用 Python JSON 或其他类似 dict 的格式,请尝试

split = data['description'].split('<br />')

d = {}

for x in split:
    key, val = x.split(': ')
    d[key] = val.strip()

Well, if the first key will not have any : in them, the following code should work好吧,如果第一个键没有任何:在其中,下面的代码应该可以工作

text = """'description': 'ALERT LEVEL: Not Applicable <br />LOCATION: Turnbulls Lane, Moruya, NSW 2537 <br />COUNCIL AREA: Eurobodalla <br />STATUS: Out of control <br />TYPE: Structure Fire <br />FIRE: Yes <br />SIZE: 0 ha <br />RESPONSIBLE AGENCY: Fire and Rescue NSW <br />UPDATED: 3 Apr 2020 21:14'"""
lines = text.split("<br />")
data_dump = {}
for line in lines:
    line = line.replace("'","")
    key,value = line.split(":", maxsplit=1)

    data_dump[key] = value
print(data_dump)

But this approach might break, if there was a : in the key of the text.但是,如果文本的键中有: ,这种方法可能会中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM