1
for replay_data in raw_replay_data['data']['video_info']:
    target.write("Coordinates: " + replay_data['lat'] + (',') + replay_data['lnt'] + "\n")

This for loop gives me fx. the output of:

Coordinates: 0,0
Coordinates: 0,0
Coordinates: 0,0
Coordinates: 0,0

I would like a number after each "Coordinates" string that increases for each new coordinates available. How can I make it output:

Coordinates 1: 0,0
Coordinates 2: 0,0
Coordinates 3: 0,0
Coordinates 4: 0,0
1
  • If you've found an answer useful, please consider accepting it by clicking on the tick mark next to it, so that others may find it useful in the future :) Commented Jan 11, 2018 at 17:51

2 Answers 2

4

Look up the enumerate() function, it's your best friend in for loops. Also you can use some str.format() function to make your output more readable:

for index, replay_data in enumerate(raw_replay_data['data']['video_info']):
    target.write("Coordinates {0}: {1}, {2}\n".format(index+1, replay_data['lat'], replay_data['lnt'])
Sign up to request clarification or add additional context in comments.

Comments

1

You create a counter and count up:

numbr = 1   # counter, starting at 1
for replay_data in raw_replay_data['data']['video_info']:
    target.write("Coordinates " + str(numbr) + ": " + replay_data['lat'] + (',') + replay_data['lnt'] + "\n")
    numbr += 1 # increment counter

You might want to look into string format methods: string.format

You can do things like:

replay_data = {}
replay_data['lat'] = 1
replay_data['lnt'] = 2

numbr = 22

print("Coordinates {n}: {lat},{lng}\n"
      .format(n=numbr, lat = replay_data['lat'], lng = replay_data['lnt']))

Output:

Coordinates 22: 1,2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.