toads_tf
toads_tf

Reputation: 128

Parts of a large string are not being replaced by the str.replace() function

I am trying to make a program that gets coordinates and puts them into the places where the variables are. This goes gets the whole document of the prefab, as a list of lines, then put it together, all of that working. The string is, of course, very big, but I'm trying to make it replace all the variables in the prefab with the values of the variables in the program, like so:

values.replace('x1 ',str(x1))
values.replace('x2 ',str(x2))
values.replace('x3 ',str(x3))
values.replace('x4 ',str(x4))
values.replace('x5 ',str(x5))
values.replace('x6 ',str(x6))
values.replace('x7 ',str(x7))
values.replace('x8 ',str(x8))
values.replace('y1 ',str(y1))
values.replace('y2 ',str(y2))
values.replace('y3 ',str(y3))
values.replace('y4 ',str(y4))
values.replace('y5 ',str(y5))
values.replace('y6 ',str(y6))
values.replace('y7 ',str(y7))
values.replace('y8 ',str(y8))
values.replace(' z1',str(z1))
values.replace(' z2',str(z2))
values.replace(' z3',str(z3))
values.replace(' z4',str(z4))
values.replace(' z5',str(z5))
values.replace(' z6',str(z6))
values.replace(' z7',str(z7))
values.replace(' z8',str(z8))

However, when I print(values) it just gives the original string, looking like this instead of having numbers where the variables should be.

    solid
{
    "id" "4"
    side
    {
        "id" "7"
        "plane" "(x1 y1 z1) (x2 y2 z2) (x3 y3 z3)"
        "material" "DEV/DEV_BLENDMEASURE"
        "uaxis" "[1 0 0 0] 0.25"
        "vaxis" "[0 -1 0 0] 0.25"
        "rotation" "0"
        "lightmapscale" "16"
        "smoothing_groups" "0"
    }
    side
    {
        "id" "8"
        "plane" "(x6 y6 z6) (x5 y5 z5) (x4 y4 z4)"
        "material" "DEV/DEV_BLENDMEASURE"
        "uaxis" "[1 0 0 0] 0.25"
        "vaxis" "[0 -1 0 0] 0.25"
        "rotation" "0"
        "lightmapscale" "16"
        "smoothing_groups" "0"
    }
    side
    {
        "id" "9"
        "plane" "(x2 y2 z2) (x4 y4 z4) (x5 y5 z5)"
        "material" "DEV/DEV_BLENDMEASURE"
        "uaxis" "[0 1 0 0] 0.25"
        "vaxis" "[0 0 -1 0] 0.25"
        "rotation" "0"
        "lightmapscale" "16"
        "smoothing_groups" "0"
    }
    side
    {
        "id" "10"
        "plane" "(x8 y8 z8) (x6 y6 z6) (x7 y7 z7)"
        "material" "DEV/DEV_BLENDMEASURE"
        "uaxis" "[0 1 0 0] 0.25"
        "vaxis" "[0 0 -1 0] 0.25"
        "rotation" "0"
        "lightmapscale" "16"
        "smoothing_groups" "0"
    }
    side
    {
        "id" "11"
        "plane" "(x3 y3 z3) (x5 y5 z5) (x6 y6 z6)"
        "material" "DEV/DEV_BLENDMEASURE"
        "uaxis" "[1 0 0 0] 0.25"
        "vaxis" "[0 0 -1 0] 0.25"
        "rotation" "0"
        "lightmapscale" "16"
        "smoothing_groups" "0"
    }
    side
    {
        "id" "12"
        "plane" "(x1 y1 z1) (x7 y7 z7) (x4 y4 z4)"
        "material" "DEV/DEV_BLENDMEASURE"
        "uaxis" "[1 0 0 0] 0.25"
        "vaxis" "[0 0 -1 0] 0.25"
        "rotation" "0"
        "lightmapscale" "16"
        "smoothing_groups" "0"
    }
    editor
    {
        "color" "0 137 246"
        "visgroupshown" "1"
        "visgroupautoshown" "1"
    }
}

Upvotes: 2

Views: 72

Answers (3)

Håken Lid
Håken Lid

Reputation: 23064

Have you considered using python's str.format() instead?

template = """
    side
    {{
        "id" "8"
        "plane" "({x6} {y6} {z6}) ({x5} {y5} {z5}) ({x4} {y4} {z4})"
        "material" "DEV/DEV_BLENDMEASURE"
        "uaxis" "[1 0 0 0] 0.25"
        "vaxis" "[0 -1 0 0] 0.25"
        "rotation" "0"
        "lightmapscale" "16"
        "smoothing_groups" "0"
    }}
"""
values = {
    'x6': 271, 'y6': 245, 'z6': 199,
    'x5': 381, 'y5': 923, 'z5': 268,
    'x4': 183, 'y4': 159, 'z4': 241,
}
print(template.format(**values))

output:

   side
    {
        "id" "8"
        "plane" "(271 245 199) (381 923 268) (183 159 241)"
        "material" "DEV/DEV_BLENDMEASURE"
        "uaxis" "[1 0 0 0] 0.25"
        "vaxis" "[0 -1 0 0] 0.25"
        "rotation" "0"
        "lightmapscale" "16"
        "smoothing_groups" "0"
    }

Upvotes: 1

Replace return the new value, then you should do:

values = values.replace('x1 ',str(x1))
...

Keep in mind that in python strings are immutable.

Upvotes: 1

Clodion
Clodion

Reputation: 1017

str.replace() return the result:

result = values.replace(…)

(and do not modify values)

Upvotes: 0

Related Questions