S.solex S.solex - 6 months ago 25
Python Question

Python csv file / next() / while loop

This should be really easy, but as i am still a noob i'm stuck.

While loop is running over an amount of time steps. I want to insert some variables from a csv file, every cell in the csv file has 3 integers(x,y,angle), which have to be sperated and passed to the simulation.

This is my try, but as you see i still have to learn a lot, but maybe someone can help me with this problem.

...........step = 0
while traci.simulation.getMinExpectedNumber() > 0:
simulationStep()
with open('myfile.csv', 'r') as f:
lines = f.next()
new = lines.split(",",)
x = new[0]
y = new[1]
angle = new[2]

vehicle.moveTo(vehid01, x, y, angle)
step += 1........


I want to store one line per time step; pass these variables. So when the next time step begins, the next line of the csv file should be read, passed and so on. So new timestep, new line does not work.x,y angle values won't change.

Any suggestions? Thanks for your help

Answer

Does this work?

...........step = 0

with open('myfile.csv', 'r') as f:
    while traci.simulation.getMinExpectedNumber() > 0:
        simulationStep()
        lines = next(f)  # compatible with Python 3
        new = lines.split(",")
        x = new[0]
        y = new[1]
        angle = new[2]   

        vehicle.moveTo(vehid01, x, y, angle)  
        step += 1........