Reputation:
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
Reputation: 1600
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)
'Bob.Smith; John.Long; Matt.Ball'
Upvotes: 3
Reputation: 3851
'; '.join(['.'.join(n.strip().split(' ')) for n in names.split(',')])
Upvotes: 1