-1

Currently I am bashing my head at a task that seems to be way too complicated for a beginner in python as myself. I'd be really grateful if you could give me a hand in this. I am trying to replace a comma with a dot. I want it done only for a particular element in a SQL script (the bold value):

Insert into SEC_DATA (HEADER_ID,TIMESTMP,BOMLABEL,TI_TO_EX,HOLD_TI,STRIKE,STRIKE_FORM,SDVALUE,VALUE1,VALUE2) values ('Swaption-Volatilitдten','02/03/2016 00:00:00','DD/MM/YYYY HH24:MI:SS'),'BID','5400','10800','0','D','**0,595**','0','0');

Sooo, I decided to use regular expressions and did the following thing:

x = "Insert into SEC_DATA (HEADER_ID,TIMESTMP,BOMLABEL,TI_TO_EX,HOLD_TI,STRIKE,STRIKE_FORM,SDVALUE,VALUE1,VALUE2) 
values ('Swaption-Volatilitдten','02/03/2016 00:00:00','DD/MM/YYYY HH24:MI:SS'),'BID','5400','10800','0','D','0,595','0','0');"

var = re.sub(r"(\d),(\d)", r"$1.$2", x)

I am expecting the $1 and $2 to keep the group unchanged, for I only want to turn the comma into a dot. The numbers around the comma are only to make sure that this is the right comma.

However, whatever I do it always results in a change of the surrounding numbers, too:

...('Swaption-Volatilitдten',to_date('02/03/2016 00:00:00','DD/MM/YYYY HH24:MI:SS'),'BID','5400','10800','0','D','**$1.$2**95','0','0');

How can I fix that?

Thanks in advance.

nesh

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
nesh
  • 1
  • 2
  • Possible duplicate of [Using a regular expression to replace upper case repeated letters in python with a single lowercase letter](https://stackoverflow.com/questions/4145451/using-a-regular-expression-to-replace-upper-case-repeated-letters-in-python-with) – Wiktor Stribiżew Oct 29 '19 at 08:34

1 Answers1

0

So it appears that a lambda function here would do the job. The solution looks like that:

import re
x = "Insert into SEC_DATA (HEADER_ID,TIMESTMP,BOMLABEL,TI_TO_EX,HOLD_TI,STRIKE,STRIKE_FORM,SDVALUE,VALUE1,VALUE2) values ('Swaption-Volatilitäten',to_date('02/03/2016 00:00:00','DD/MM/YYYY HH24:MI:SS'),'BID','5400','10800','0','D','0,595','0','0');"
var = re.sub(r"(\d),(\d)", lambda match: "%s.%s" % (match.group(1), match.group(2)), x)

Contributor: User pts gave an accurate answer to a similar question

Thanks and cheers!

nesh
  • 1
  • 2