删除字符前的空格有困难。我试图解析下面的样本串对象:删除字符串前的空格
Mary Whitten: {laptop: 100, tv : 5, xbox: 50, }, James Doe: {laptop: 30, tv: 3, xbox: 5,}, Jesus Navas: {laptop: 25, tv: 3, xbox: 5},Matt Mart:{laptop:10, xbox: 10}
我也使用.split(“},”)在解析上面的字符串。我拿了钥匙:
d=['Mary Whitten', ' James Doe', ' Jesus Navas', 'Matt Mart']
有在“詹姆斯·多伊”和“耶稣纳瓦斯”我正努力避免因键是敏感的空格,即d [“詹姆斯·多伊”的前面一些空格]与['James Doe']不同,因为密钥中有空格。我将如何避免它?另外,我想使列表项不区分大小写,如:
d=(items.lowercase() for items in d)
您可以使用'strip'。 [修剪字符串](http://stackoverflow.com/a/6039813/3336968) – fredtantini
你有没有试过把它解析为['json'](http://docs.python.org/2/library/json。 html)字符串? –
下面的答案是正确的,但我不禁想知道你从哪里得到这个字符串。它似乎想成为json,但并不完全。你是否能够改变字符串的生成方式,以便于解析? –