'I have a text file with a lot of numbers on new line and am wanting to generate the same line of code with the different values
So basically I have a Map setup and it takes coordinates to place Blips on the map. I have a long list of coordinates that are on different lines which look like this - X, Y
-329, -1562
-357, -1544
-358, -1547
345, -1553
-355, -1552
485, -1274
463, -1305
577, -1930
575, -1928
461, -1973
103, -1808
I am wanting to make it so every X and Y value that is on a line is taken from the TXT file and generated into this text
var X = X VALUE HERE;
var Y = Y VALUE HERE;
L.marker([X, Y], { icon: ingredientIcon(1) }).addTo(Icons["Crafting/Items"]).bindPopup("Text");
var X = 2nd X VALUE HERE;
var Y = 2nd Y VALUE HERE;
L.marker([X, Y], { icon: ingredientIcon(1) }).addTo(Icons["Crafting/Items"]).bindPopup("Text");
I figured Python would be the best way to generate it but I have no clue. I'm just trying to make my life easier and have no clue where to start. I cant seem to figure it out and have done a bit of research. Please save me.
Solution 1:[1]
Try this:
output = ""
with open("file path to read from", "r") as file:
for line in file.readlines():
line = line.strip()
x, y = line.split(", ")
output += f"""\nvar X = {x};
var Y = {y};
L.marker([X, Y], \{ icon: ingredientIcon(1) }).addTo(Icons["Crafting/Items"]).bindPopup("Text");
var X = 2nd X VALUE HERE;
var Y = 2nd Y VALUE HERE;
L.marker([X, Y], \{ icon: ingredientIcon(1) }).addTo(Icons["Crafting/Items"]).bindPopup("Text");"""
with open("file path to write to", "w") as file:
file.write(output)
I recommend you try this with a different file, because this can override your original file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CozyCode |
