I am having difficulty removing white space in front of the character. I am trying to parse the sample string object below:
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}
i also used .split('},') while parsing the above string. The keys that I got:
d=['Mary Whitten', ' James Doe', ' Jesus Navas', 'Matt Mart']
there are some white spaces in front of ' James Doe' and ' Jesus Navas' which I am trying to avoid because keys are sensitive to white spaces i.e d['James Doe'] is not same as [' James Doe'] because of the white space in the key. How would I avoid it? Also, I wanted to make the list items case insensitive like:
d=(items.lowercase() for items in d)
Answer
You can simply do that as follows:
string.strip()
Running this script on " Hello "
would give "Hello"
For your code, you would do it as follows:
d = [item.strip() for item in d]
>>> d = (item.lower().strip() for item in d)
>>> print d
['mary whitten', 'james doe', 'jesus navas', 'matt mart']
No comments:
Post a Comment