user18954171
user18954171

Reputation:

Parse a list of names from 'First Last' to 'First.Last'

I'm trying to take a text based list of names and convert it to a format for active directory.

My goal is to have a string like this:

"Bob Smith, John Long, Matt Ball"

into

"Bob.Smith; John.Long; Matt.Ball"

So far I've done:

names = 'Bob Smith, John Long, Matt Ball'
x = names.replace(',', ';')
print(x.replace(' ', ''))

With the result:

"BobSmith;JohnLong;MattBall"

How can I achieve what I'm looking for?

Upvotes: 0

Views: 48

Answers (2)

dtlam26
dtlam26

Reputation: 1600

Change

You have to make just a small adjustment on what is been replaced:

string = 'Bob Smith, John Long, Matt Ball'
string = string.replace(' ', '.').replace(',.', '; ')
print(string)

Result

'Bob.Smith; John.Long; Matt.Ball'

Upvotes: 3

Andrey Lukyanenko
Andrey Lukyanenko

Reputation: 3851

'; '.join(['.'.join(n.strip().split(' ')) for n in names.split(',')])

Upvotes: 1

Related Questions