У меня есть переменная first_line
в формате 1888,60,-32
, и я хочу разделить ее, например, на x = 1888
y = 60
и z = -32
, но они могут быть различной длинынапример, другой - 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
(выход)
{
"id": "END_1888,60,-32",
"name": "END",
"icon": "waypoint-normal.png",
"x": some value x,
"y": some value y,
"z": some value z,
}