将变量分成3个变量,然后删除零件文本

时间:2019-02-05 16:34:06

标签: python python-3.x variables

我有一个变量first_line,其格式为1888,60,-32,例如,我想将其分隔为等于x = 1888 y = 60和{{ 1}},但是它们的长度可能不同,例如,另一个是z = -32

我有tried this,并且不允许我将文本拆分为变量。

768,60,-13776

write.py

# open current file and read first line with open(currentfile) as f: first_line = f.readline() first_line = first_line.rstrip() print(currentfile) print(first_line) # define fullnamejson as END + first_line + .json fullnamejson = "END_" + first_line + ".json" # define fullname as END + first_line fullname = "END_" + first_line os.rename(currentfile, fullnamejson) print(fullnamejson) # define x y and z x = "some value x" y = "some value y" z = "some value z" # define formatted as what will be written to the file formatted = "{\n \"id\": \"" + fullname + "\",\n \"name\": \"END\",\n \"icon\": \"waypoint-normal.png\",\n \"x\": " + x + ",\n \"y\": " + y + ",\n \"z\": " + z + ",\n}" print(formatted) # write to file with open(fullnamejson, "w") as text_file: ##print(f(fullnamejson), file=text_file) print(f'{formatted}', file=text_file) (输入)

zzz_split_1.txt

1888,60,-32 (输出)

fullnamejson

3 个答案:

答案 0 :(得分:1)

您在寻找这个吗?

coords = first_line.split(",")
x, y, z = int(coords[0]), int(coords[1]), int(coords[2])

答案 1 :(得分:0)

1。您可以使用函数split分割字符串。 2.您可以将一个集合的表达式结果分配给多个变量。

上面编写的所有代码看起来都像这样:

x, y, z =your_string.split(',')

只需更改引号。(从iPhone编写)

答案 2 :(得分:0)

亲密无间,您所要做的就是使用if (jUser.active === 1) { $('#lblActive').text("yes") } if (jUser.active === 0) { $('#lblActive').text("no") } 来分割内容!

split