1

I'm looking to remove a ',' (comma) from a string, but only the second time the comma occurs as it needs to be in the correct format for reverse geocoding...

As an example I have the following string in python:

43,14,3085

How would I convert it to the following format:

43,143085

I have tried using regex and str.split() but have not achieved result yet..

3 Answers3

2

If you're sure that string only contains two commas and you want to remove the last one you can use rsplit with join:

>>> s = '43,14,3085'
>>> ''.join(s.rsplit(',', 1))
'43,143085'

In above rsplit splits starting from the end number of times given as a second parameter:

>>> parts = s.rsplit(',', 1)
>>> parts
['43,14', '3085']

Then join is used to combine the parts together:

>>> ''.join(parts)
'43,143085'
niemmi
  • 17,113
  • 7
  • 35
  • 42
0

This will get rid of all your commas excepts the first one:

string = '43,14,3085'
splited = string.split(',')
string=",".join(splited[0:2])
string+="".join(splited[2:])
print(string)
kda
  • 194
  • 11
0

What about something like:

i = s.find(',')
s[:i] + ',' + s[i+1:].replace(",", "")
Kevin
  • 1,870
  • 2
  • 20
  • 22