Home > Mobile >  Search and replace specific strings with floating point representations in python
Search and replace specific strings with floating point representations in python

Time:05-13

Problem: I'm trying to replace mutiple specific sequences in a string with another mutiple specific sequences of floating point representations in Python.

I have an array of strings in a JSON-file, which I load to a python script via the json-module. The array of strings:

{
  "LinesToReplace": [
    "_ __ ___ ____ _____ ______ _______      ",
    "_._ __._ ___._ ____._ _____._ ______._  ",
    "_._ _.__ _.___ _.____ _._____ _.______  ",
    "_._ __.__ ___.___ ____.____ _____._____ ",
    "_. __. ___. ____. _____. ______.        "
  ]
}

I load the JSON-file via the json-module:

with open("myFile.json") as jsonFile:
  data = json.load(jsonFile)

I'm trying to replace the sequences of _ with specific substrings of floating point representations.

Specification:

  • Character to find in a string must be a single _ or a sequence of multiple _.
  • The length of the sequence of _ is unknown.
  • If a single _ or a sequence of multiple _ is followed by a ., which is again followed by a single _ or a sequence of multiple _, the . is part of the _-sequence.
  • The . is used to specify decimals
  • If the . isn’t followed by a single _ or a sequence of multiple _, the . is not part of the _-sequence.
  • The sequence of _ and . is to be replaced by floating point representations, i.e.,
  • Related