简体   繁体   English

将字符串转换为字符,其中键不是字符串

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

I am parsing an html file an I end up with something that almost looks like a python dictionary... except for the keys not being strings. 我解析HTML文件的我最终的东西, 几乎看起来像一条巨蟒字典...除了键不是字符串。 Because of this I cannot use ast.literal_eval() or json.loads() . 因此我不能使用ast.literal_eval()json.loads()

The string looks like this: 字符串看起来像这样:

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'}'

I want to convert this into a python dictionary where the keys are also strings. 我想将其转换为python字典,其中键也是字符串。 I think this need some good ol' regex but I don't know how to do it. 我认为这需要一些好的'正则表达式,但我不知道该怎么做。

也许你可以从类似的问题中关注@Zero Piraeus的建议,并使用像PyYAML这样的YAML解析器来读取你的字符串,然后将结果转换为字典。

暂无
暂无

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

相关问题 创建字典,其中值是转换为字符串的键 - Create dictionary where values are keys transformed to strings 将作为整数字符串的JSON字典键转换为整数 - Convert keys of JSON dictionary that are strings of ints to ints 给定一个 [string, number] 元组列表,创建一个字典,其中键是字符串的第一个字符,值是数字的总和 - Given a list of [string, number] tuples, create a dictionary where keys are the first characters of strings and the values are sums of the numbers 将字符串的字符与字典的键匹配,如果匹配,则将字符串转换为键的值 - Matching characters of a string with keys of a dictionary and converting strings to values of keys if matched 将String转换为多级字典键? - Convert String to multi-level dictionary keys? 在python中将字符串转换为给定字典的键 - Convert a string to keys of a given dictionary in python 如何访问 python 中的字典元素,其中键是字节而不是字符串 - How to access elements of a dictionary in python where the keys are bytes instead of strings 给定一个以字符串列表作为其值的字典,您将如何检索列表包含所有其他列表唯一的字符串的所有键? - Given a dictionary with lists of strings as their values, how would you retrieve all keys where the list contains a string unique to all other lists? 如何将嵌套的字典键转换为字符串? - How can I convert nested dictionary keys to strings? Python:将包含时间戳的字典键列表转换为字符串列表 - Python: convert list of dictionary keys containing timestamps into a list of strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM