Help TypeError: string indices must be integers in Python


#1

Hello

I am very new to programming and I am having a problem with the following:

I have a very simple and perhaps pointless task whereby I am trying to take longitude and latitude data from a JSON API and write to a CSV file. I have based the code on another that seems to work OK.

I keep getting the following error code: _
c.writerow([item['latitude']])_TypeError: string indices must be integers

Can anyone help me??

1.JSON API from http://api.open-notify.org/iss-now.json

{
"iss_position": {
"latitude": 17.03894678089794,
"longitude": 1.17550020887323
},
"message": "success",
"timestamp": 1463137065
}

2.My Python Code

import requests
import csv

r = requests.get("http://api.open-notify.org/iss-now.json").json()

c = csv.writer(open("ISS.csv", "w"),lineterminator='\n')

for item in r['iss_position']:

    c.writerow([item['latitude'],['longitude']])

3.Error Message

C:\Python35\python.exe C:/Users/Ian/PycharmProjects/ISS/ISS.py
Traceback (most recent call last):
File "C:/Users/Ian/PycharmProjects/ISS/ISS.py", line 10, in
c.writerow([item['latitude']])
TypeError: string indices must be integers

Process finished with exit code 1


Replace this line with your code.


#2

'hello'[3]  # OK
'hello'['kebab']  # error