comas with quote mark right behind

Question:

I need to format a string dictionary before convert it into dict
I tried it with regex:

        decmark_reg = re.compile('(?<=d),(?=d)')
        dict_decmark = decmark_reg.sub('.',dict_quotes)
        convertedDict = json.loads(dict_decmark)

but then I realized it mess with "fingerOne_OffTimes" values

    dict_str = '{"level":0,6,     
                 "params":{
                 "startLvlTime":1114.3851318359375,
                 "fingerOne_OffTimes":[459,4716491699219,78532]}}

desired result

    dict_str = '{"level":0.6,                                      # 0,6 -> 0.6
                 "params":{
                 "startLvlTime":1114.3851318359375,
                 "fingerOne_OffTimes":[459,4716491699219,78532]}}  # no change

Would need a pattern that detect all comas but ones which have quot mark right behind

Asked By: kubaSpolsky

||

Answers:

You can match strings that consist of digits and at least two commas, and then match your pattern in all other contexts:

decmark_reg = re.compile(r'(d+(?:,d+){2,})|(?<=d),(?=d)')
dict_decmark = decmark_reg.sub(lambda x: x.group(1) or '.', dict_quotes)

Details:

  • (d+(?:,d+){2,}) – Group 1: one or more digits and then two or more sequences of a comma and one or more digits
  • | – or
  • (?<=d),(?=d) – a comma that is enclosed with digits on both ends

If Group 1 matches, its value is returned, else, the . is returned.

Answered By: Wiktor Stribiżew

Try (regex101):

import re

dict_str = """
{"level":0,6,     
 "params": {
 "startLvlTime":1114.3851318359375,
 "fingerOne_OffTimes":[459,4716491699219,78532]}}"""

pat = re.compile(r"([.*?])|(d,d)")

dict_str = pat.sub(
    lambda g: g.group(1) or g.group(2).replace(",", "."), dict_str
)
print(dict_str)

Prints:

{"level":0.6,     
 "params": {
    "startLvlTime":1114.3851318359375,
    "fingerOne_OffTimes":[459,4716491699219,78532]}}
Answered By: Andrej Kesely
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.