Home > Enterprise >  Python nested loop generate coordinate and save them to csv file
Python nested loop generate coordinate and save them to csv file

Time:06-12

I hope somebody can shade some light here and help me to figure out what I am doing wrong.

I am working with Ursina engine to generate some some cube to a given location, as follow

terrain_z_x = 10
terrain_depth = 3

for z in range(terrain_z_x):
    for y in range(terrain_depth):
        for x in range(terrain_z_x):
            if y > 0:
                voxel = Voxel(position=(x,-y,z))
            else:
                voxel = Voxel(position=(x,-y,z))
            terrain_dictionary["Object"] = "Cube"
            terrain_dictionary["x"] = voxel.position.x
            terrain_dictionary["y"] = voxel.position.y
            terrain_dictionary["z"] = voxel.position.z

This generate 10 cubes on the z and x axis and 3 on the y. This works perfectly and I don't have any issue. But what I wanted to do, is while I am looping over those coordinates to save them into a csv file to have a list of all the cubes that are present in the engine.

so I imported csv library and updated my code as follow.

for z in range(terrain_z_x):
    for y in range(terrain_depth):
        for x in range(terrain_z_x):
            if y > 0:
                voxel = Voxel(position=(x,-y,z))
            else:
                voxel = Voxel(position=(x,-y,z))
            terrain_dictionary["Object"] = "Terrain"
            terrain_dictionary["x"] = voxel.position.x
            terrain_dictionary["y"] = voxel.position.y
            terrain_dictionary["z"] = voxel.position.z

with open("position.csv", 'w') as position:
    writer = csv.DictWriter(position, terrain_info)
    writer.writeheader()
    writer.writerow(terrain_dictionary)

This works but not as expected. Because on the csv file, I have only the last looped position.

Object,x,y,z
Cube,9.0,-2.0,9.0

I am having some hard time to understand how I can save each given position to the csv file while performing the loop and have a csv file like this:

Object,x,y,z
Cube,0,0,0
Cube,0,0,1
......
Cube, 9,-2,9

If hope I made my question and issue clear, if not please do not hesitate to ask more details.

Thank you so much in advance

CodePudding user response:

At the end of the triple for loop, the dictionary terrain_dictionary only has 4 keys with the last values in them. You should append each dictionary in a list of dict, then loop on this list to write each dictionary in the CSV file.

dict_list = []
for z in range(terrain_z_x):
    for y in range(terrain_depth):
        for x in range(terrain_z_x):
            if y > 0:
                voxel = Voxel(position=(x,y,z)) # I guess the - sign was a mistake ?
            else:
                voxel = Voxel(position=(x,-y,z))
            terrain_dictionary["Object"] = "Terrain"
            terrain_dictionary["x"] = voxel.position.x
            terrain_dictionary["y"] = voxel.position.y
            terrain_dictionary["z"] = voxel.position.z
            # Insert a copy of the dictionary in the list
            dict_list.append(terrain_dictionary.copy())

with open("position.csv", 'w') as position:
    writer = csv.DictWriter(position, terrain_info)
    writer.writeheader()
    for d in dict_list:
        writer.writerow(d)
  • Related