Rick Colgan
Rick Colgan

Reputation: 100

Adding a space into a certain string in python

I have data that looks like this:

SMITH,JOHN, additional data delimited by commas
JONES,TOMMY, additional data delimited by commas
WILLIAMS, BILLY, additional data delimited by commas
etc.

I need to make it look like this:

SMITH, JOHN, additional data delimited by commas
JONES, TOMMY, additional data delimited by commas
WILLIAMS, BILLY, additional data delimited by commas
etc.

Notice the extra space after the comma on the first two names.

I think I need to use a regex to make this happen, but it doesn't seem to work. I've tried:

grantor = grantor.replace(r'[A-Z],[A-Z]', r'[A-Z], [A-Z]')

but it isn't happening for me. What have I missed? I only need to change the names where there is a text character, a comma, and a text character immediately next to each other. Other names in the list are right and don't need to be changed.

Upvotes: 1

Views: 67

Answers (1)

poke
poke

Reputation: 387677

str.replace does not use regular expressions. You would have to use the re module for that.

But since you are just replacing a comma by a comma and a space, you don’t actually need regular expressions here:

s = '''SMITH,JOHN
JONES,TOMMY
etc.'''

s = s.replace(',', ', ')

print(s)
# SMITH, JOHN
# JONES, TOMMY
# etc.

I only need to replace the commas that are immediately preceded and proceeded by uppercase text characters.

Then you might want to use regular expressions after all. You will have to use re.sub for this:

import re
s = 'FOO, SMITH,JOHN'

s = re.sub('([A-Z]),([A-Z])', r'\1, \2', s)
print(s)
# FOO, SMITH, JOHN

This uses references to put in those two characters around the comma. You can also use lookbehinds and lookaheads for this:

s = 'FOO, SMITH,JOHN'

s = re.sub('(?<=[A-Z]),(?=[A-Z])', ', ', s)
print(s)
# FOO, SMITH, JOHN

Upvotes: 3

Related Questions