Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

python - how to remove extra space from infront of a dictionary's keys?

I have a dictionary. There are some space infant of keys. How I can remove all of these space and keep only one space infant of keys? I made the dictionary with the following code:

def get_JobAbbreviation_data():
    data=pd.read_csv('A.csv')
    data=data.replace(u'xa0', u'', regex=True)
    data.dropna(inplace=True)
    Title = data['FIRST'].str.lower()+' '
    Abbr = data['1ST'].str.lower()+ ' '
    JobAbbreviation=dict(zip(Abbr, Title))
    return JobAbbreviation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This breaks down to "How to remove all (but one) spaces at the start of a string"?

Removing spaces is easy:

In [1]: "    abcd".lstrip()
Out[1]: 'abcd'

Then, just add a space, so... " " + mystring.lstrip().

That's the building block. The next part of the question is: "How to apply this to all the keys?":

Abbr = [" " + s.lstrip() for s in Abbr]

Then build your dictionary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...