簡體   English   中英

將字符串轉換為字符,其中鍵不是字符串

[英]Convert string to dictionary where keys are not strings

我解析HTML文件的我最終的東西, 幾乎看起來像一條巨蟒字典...除了鍵不是字符串。 因此我不能使用ast.literal_eval()json.loads()

字符串看起來像這樣:

x = '{name:'Berlin, Germany', daypart:'night', href:'/en/de/berlin/10178/weather-forecast/178087', icon:'http://vortex.accuweather.com/adc2010/images/icons-numbered/33-m.png', bg:'cl', temp:'22',  realfeel:'22',  text:'Clear'}'

我想將其轉換為python字典,其中鍵也是字符串。 我認為這需要一些好的'正則表達式,但我不知道該怎么做。

也許你可以從類似的問題中關注@Zero Piraeus的建議,並使用像PyYAML這樣的YAML解析器來讀取你的字符串,然后將結果轉換為字典。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM