Search code examples
pythonregexcesiumjs

Text in a file replaced by regex. How to write the change into file?---Question extended


Reference

This is a Cesiumjs specific file.czml . It contains elevations in meters. These are 1407.0 and 1408.2 and 1409.6 --- and so on. The elevations are in two lines.

var czml = [{
"id" : "document",
"name" : "CZML Geometries: Polyline",
"version" : "1.0"
}
,

{snip---"cartographicDegrees": [0, 12.039186, 47.663944, 1407.0, 30.0, 12.038963, 47.663952, 1408.2, 60.0, 12.038646, 47.663924, 1409.6, 90.0, 12.038504, 47.663926, 1412.2, 120.0, snip---}
,
{
{snip--- "cartographicDegrees": [0, 12.035275, 47.661445, 1557.2, 30.0, 12.035461, 47.661414, 1550.4, 60.0, 12.03565, 47.661382, 1551.4, 90.0, 12.035831, 47.661391, 1546.6, 120.0, snip---}];

I want to add 40 to each of it, so the result should be in both lines: 1447.0 instead of 1407.0, 1448.2 instead of 1408.0, 1449.6 instead of 1409.6 --- and so on. But my code only changes the first value --- 1407.0 to 1447.0 --- in each line.

What is wrong in my following Python code?

import re
f1 = input("name of your CZML file---> ")
input_file = open(f1,'r+')
output_file = open(f1 + '_output', 'w+')
line = input_file.readline()
while line:
    res = re.search(r"([0-9]{4}\.[0-9]{1,})", line)
    #res = re.findall(r"([0-9]{4}\.[0-9]{1,})", line)   
    for i in range(len(line)):
        number = line[i : (i+6)]
        if re.search(r"([0-9]{4}\.[0-9]{1})", number):
            number=float(number)
            numberelev = number+40
            numberelev=str(numberelev)
        line = line.replace(res.group(1), numberelev)
    output_file.write(line)
    line = input_file.readline()
input_file.close()
output_file.close()
print("OK")

Thx in advance for your help, Josef


Solution

  • The problem is that the code used to replace string occurrencies, line = line.replace(res.group(1), numberelev), uses res.group(1), which does not change during the iteration over range(len(line)), because it is initialized before the loop: so, you code finds all occurrences but replaces only the first.

    In order to fix the problem, you should change the body loop as follows:

    for i in range(len(line)):
        number = line[i : (i+6)]
        find = re.search(r"([0-9]{4}\.[0-9]{1})", number)
        if find:
            number=float(number)
            numberelev = number+40
            numberelev=str(numberelev)
            line = line.replace(find.group(1), numberelev)
    

    The key point is replacing find.group(1) (which changes when the loops proceeds) and not res.group(1) (which for each line assumes only the value of the first occurrence of your pattern).

    I hope this can be helpful for you!