我正在尝试创建一个程序来获取坐标并将它们放入变量所在的位置。这将获得预制件的整个文档,作为一个行列表,然后将它们放在一起,所有这些工作。当然,字符串非常大,但我试图用预设中的变量值替换预制中的所有变量,如下所示:
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))
然而,当我 打印(值) 它只是提供原始字符串,看起来像这样,而不是变量应该是数字。
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"
}
}
答案 0 :(得分:1)
替换返回新值,然后你应该这样做:
values = values.replace('x1 ',str(x1))
...
请记住,python字符串是不可变的。
答案 1 :(得分:1)
您是否考虑过使用python的str.format()?
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))
输出:
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"
}
答案 2 :(得分:0)
str.replace()
返回结果:
result = values.replace(…)
(并且不要修改values
)