crompnk
asked on
TypeError: string indices must be integers while parsing JSON using Python
Hi,
I am parsing json to csv using Python 3.4, but I get the following TypeError and would appreciate some assistance:
Traceback (most recent call last):
File "C:\Temp\MetOfficehourlyjs on.py", line 104, in <module>
for rep in period['Rep']:
TypeError: string indices must be integers
I am parsing json to csv using Python 3.4, but I get the following TypeError and would appreciate some assistance:
Traceback (most recent call last):
File "C:\Temp\MetOfficehourlyjs
for rep in period['Rep']:
TypeError: string indices must be integers
#!/usr/bin/python
#-------------------------------------------------------------------------------------------
#Assemble a CSV file from the Hourly Observation Service from the Met Office
#DataPoint API.
#It does an API request,
#parses the data as JSON, then turns that into a set of data model objects, those
#are them flattened and written down as a CSV file.
#Known Issues:
#* Some of the data isn't filled in (data date, data type, etc).
#* Error handling just sets the field to blank.
#* There's only real error handling on the observation data.
#-------------------------------------------------------------------------------------------
import requests
import json
import csv
API_KEY = ".........................................."
URL = "http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/json/all?res=hourly&time=2014-12-29T13Z&key=%s" % (API_KEY)
OUTPUT_FILE = "C:\Temp\example_data.csv"
def remove_key(d, key):
#-------------------------------------------------------------------------------------------
#Remove a key from a dictionary.
#Return a copy of the dict.
#-------------------------------------------------------------------------------------------
r = dict(d)
del r[key]
return r
def flatten_location(loc):
#-------------------------------------------------------------------------------------------
#Flatten a Location instance in preparation for writing to a CSV.
#-------------------------------------------------------------------------------------------
location = loc.__dict__
location_info = remove_key(location, 'data')
location_data = []
for data in location['data']:
flat_data = data.__dict__
flat = dict(list(location_info.items()) + list(flat_data.items()))
location_data.append(flat)
return location_data
class Location():
#-------------------------------------------------------------------------------------------
#Data Model for locations returned from DataPoint.
#-------------------------------------------------------------------------------------------
def __init__(self):
self.data_date = ""
self.data_type = ""
self.location_id = ""
self.lat = ""
self.lon = ""
self.name = ""
self.country = ""
self.continent = ""
self.data = []
def __repr__(self):
return "<Location: %s>" % (self.name)
class Data():
#-------------------------------------------------------------------------------------------
#Data Model for collected data returned from DataPoint
#-------------------------------------------------------------------------------------------
def __init__(self):
self.collection_time = ""
self.wind_direction = ""
self.pressure = ""
self.wind_speed = ""
self.temperature = ""
self.visibility = ""
self.weather_type = ""
def __repr__(self):
return "<Data: %s>" % (self.collection_time)
# fetch data, parse json
r = requests.get(URL)
j = r.json()
# get a collection of locations
locations = j['SiteRep']['DV']['Location']
parsed_locations = []
for location in locations:
# turn the locations into Location objects
loc = Location()
loc.location_id = location['i']
loc.lat = location['lat']
loc.lon = location['lon']
loc.name = location['name']
loc.country = location['country']
loc.continent = location['continent']
# turn the location's data into Data objects
for period in location['Period']:
for rep in period['Rep']:
data = Data()
data.collection_time = rep['$']
try:
data.wind_direction = rep['D']
except KeyError:
data.wind_direction = ""
try:
data.pressure = rep['P']
except KeyError:
data.pressure = ""
try:
data.wind_speed = rep['S']
except KeyError:
data.wind_speed = ""
try:
data.temperature = rep['T']
except KeyError:
data.temperature = ""
try:
data.visibility = rep['V']
except KeyError:
data.visibility = ""
try:
data.weather_type = rep['W']
except KeyError:
data.weather_type = ""
loc.data.append(data)
# hold onto a flattened version of the location
parsed_locations.extend(flatten_location(loc))
# write the csv
with open(OUTPUT_FILE, "wt") as f:
writer = csv.DictWriter(f, parsed_locations[0].keys())
writer.writeheader()
for d in parsed_locations:
writer.writerow(d)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thanks again, I made the changes and the script runs without errors, however it only exports the last rep record in each location object and not all rep records in each location>>period array.
Do I still need the class Period() ?
Here is the Python script so far:
Here is how the json looks:
Thanks again, I made the changes and the script runs without errors, however it only exports the last rep record in each location object and not all rep records in each location>>period array.
Do I still need the class Period() ?
Here is the Python script so far:
#!/usr/bin/python
#-------------------------------------------------------------------------------------------
#Assemble a CSV file from the Hourly Observation Service from the Met Office
#DataPoint API.
#It does an API request,
#parses the data as JSON, then turns that into a set of data model objects, those
#are them flattened and written down as a CSV file.
#Known Issues:
#* Some of the data isn't filled in (data date, data type, etc).
#* Error handling just sets the field to blank.
#* There's only real error handling on the observation data.
#-------------------------------------------------------------------------------------------
import requests
import json
import csv
API_KEY = ""
#* Example: to obtain observations for every location in the UK at all available times in JSON format:
URL = "http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/json/all?res=hourly&key=%s" % (API_KEY)
#* Example: to obtain observations for every location in the UK at a particular time in JSON format:
#URL = "http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/json/all?res=hourly&time=2014-12-29T23Z&key=%s" % (API_KEY)
#* Example: to obtain observations for a specified location at all available times in JSON format:
#URL = "http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/json/3002?res=hourly&key=%s" % (API_KEY)
OUTPUT_FILE = "C:\Temp\example_data.csv"
def remove_key(d, key):
#-------------------------------------------------------------------------------------------
#Remove a key from a dictionary.
#Return a copy of the dict.
#-------------------------------------------------------------------------------------------
r = dict(d)
del r[key]
return r
def flatten_location(loc):
#-------------------------------------------------------------------------------------------
#Flatten a Location instance in preparation for writing to a CSV.
#-------------------------------------------------------------------------------------------
location = loc.__dict__
location_info = remove_key(location, 'data')
location_data = []
for data in location['data']:
flat_data = data.__dict__
flat = dict(list(location_info.items()) + list(flat_data.items()))
location_data.append(flat)
return location_data
class Location():
#-------------------------------------------------------------------------------------------
#Data Model for locations returned from DataPoint.
#-------------------------------------------------------------------------------------------
def __init__(self):
self.location_id = ""
self.lat = ""
self.lon = ""
self.elevation = ""
self.name = ""
self.country = ""
self.continent = ""
self.data = []
def __repr__(self):
return "<Location: %s>" % (self.name)
class Period():
#-------------------------------------------------------------------------------------------
#Data Model for period data returned from DataPoint.
#-------------------------------------------------------------------------------------------
def __init__(self):
self.data_date = ""
self.data_type = ""
def __repr__(self):
return "<Period: %s>" % (self.name)
class Data():
#-------------------------------------------------------------------------------------------
#Data Model for collected data returned from DataPoint
#-------------------------------------------------------------------------------------------
def __init__(self):
self.collection_time = ""
self.wind_direction = ""
self.pressure = ""
self.wind_speed = ""
self.temperature = ""
self.visibility = ""
self.weather_type = ""
self.wind_gust = ""
self.pressure_tendency = ""
self.dew_point = ""
self.screen_relative_humidity = ""
def __repr__(self):
return "<Data: %s>" % (self.collection_time)
# fetch data, parse json
r = requests.get(URL)
j = r.json()
# get a collection of locations
locations = j['SiteRep']['DV']['Location']
parsed_locations = []
for location in locations:
# turn the locations into Location objects
loc = Location()
loc.location_id = location['i']
loc.lat = location['lat']
loc.lon = location['lon']
loc.elevation = location['elevation']
loc.name = location['name']
loc.country = location['country']
loc.continent = location['continent']
# turn the location's data into Data objects
periods = location['Period']
if hasattr(periods, "keys"):
periods = list((periods,))
for period in periods:
reps = period['Rep']
if hasattr(reps, "keys"):
reps = list((reps,))
for rep in reps:
data = Data()
data.data_type = period['type']
data.data_date = period['value']
data.collection_time = rep['$']
data.wind_direction = rep.get('D', "")
data.pressure = rep.get('P',"")
data.wind_speed = rep.get('S',"")
data.temperature = rep.get('T',"")
data.visibility = rep.get('V',"")
data.weather_type = rep.get('W',"")
data.wind_gust = rep.get('G',"")
data.pressure_tendency = rep.get('Pt',"")
data.dew_point = rep.get('C',"")
data.screen_relative_humidity = rep.get('H',"")
loc.data.append(data)
# hold onto a flattened version of the location
parsed_locations.extend(flatten_location(loc))
# write the csv
with open(OUTPUT_FILE, "wt") as f:
writer = csv.DictWriter(f, parsed_locations[0].keys())
writer.writeheader()
for d in parsed_locations:
writer.writerow(d)
Here is how the json looks:
{
"SiteRep":{
"Wx":{
"Param":[
{
"name":"G",
"units":"mph",
"$":"Wind Gust"
},
{
"name":"T",
"units":"C",
"$":"Temperature"
},
{
"name":"V",
"units":"m",
"$":"Visibility"
},
{
"name":"D",
"units":"compass",
"$":"Wind Direction"
},
{
"name":"S",
"units":"mph",
"$":"Wind Speed"
},
{
"name":"W",
"units":"",
"$":"Weather Type"
},
{
"name":"P",
"units":"hpa",
"$":"Pressure"
},
{
"name":"Pt",
"units":"Pa\/s",
"$":"Pressure Tendency"
},
{
"name":"Dp",
"units":"C",
"$":"Dew Point"
},
{
"name":"H",
"units":"%",
"$":"Screen Relative Humidity"
}
]
},
"DV":{
"dataDate":"2015-01-06T15:00:00Z",
"type":"Obs",
"Location":[
{
"i":"3002",
"lat":"60.749",
"lon":"-0.854",
"name":"BALTASOUND",
"country":"SCOTLAND",
"continent":"EUROPE",
"elevation":"15.0",
"Period":[
{
"type":"Day",
"value":"2015-01-05Z",
"Rep":[
{
"D":"S",
"H":"90.9",
"P":"1012",
"S":"16",
"T":"8.3",
"V":"15000",
"W":"2",
"Pt":"F",
"Dp":"6.9",
"$":"900"
},
{
"D":"S",
"H":"91.5",
"P":"1012",
"S":"16",
"T":"8.5",
"V":"17000",
"W":"7",
"Pt":"F",
"Dp":"7.2",
"$":"960"
},
{
"D":"SSW",
"H":"89.6",
"P":"1012",
"S":"17",
"T":"8.5",
"V":"15000",
"W":"7",
"Pt":"F",
"Dp":"6.9",
"$":"1020"
},
{
"D":"SSW",
"H":"89.6",
"P":"1011",
"S":"16",
"T":"8.4",
"V":"14000",
"W":"7",
"Pt":"F",
"Dp":"6.8",
"$":"1080"
},
{
"D":"SSW",
"H":"87.0",
"P":"1011",
"S":"11",
"T":"8.4",
"V":"16000",
"W":"8",
"Pt":"F",
"Dp":"6.4",
"$":"1140"
},
{
"D":"S",
"H":"91.5",
"P":"1011",
"S":"15",
"T":"8.2",
"V":"11000",
"W":"7",
"Pt":"F",
"Dp":"6.9",
"$":"1200"
},
{
"D":"S",
"H":"90.1",
"P":"1010",
"S":"18",
"T":"8.4",
"V":"14000",
"W":"8",
"Pt":"F",
"Dp":"6.9",
"$":"1260"
},
{
"D":"SSW",
"H":"90.2",
"P":"1010",
"S":"17",
"T":"8.3",
"V":"12000",
"W":"7",
"Pt":"F",
"Dp":"6.8",
"$":"1320"
},
{
"D":"SSW",
"H":"88.9",
"P":"1009",
"S":"16",
"T":"8.4",
"V":"11000",
"W":"8",
"Pt":"F",
"Dp":"6.7",
"$":"1380"
}
]
},
{
"type":"Day",
"value":"2015-01-06Z",
"Rep":[
{
"D":"S",
"H":"90.2",
"P":"1008",
"S":"15",
"T":"8.4",
"V":"15000",
"W":"8",
"Pt":"F",
"Dp":"6.9",
"$":"0"
},
{
"D":"SSW",
"H":"90.9",
"P":"1007",
"S":"15",
"T":"8.1",
"V":"13000",
"W":"7",
"Pt":"F",
"Dp":"6.7",
"$":"60"
},
{
"D":"SSW",
"H":"92.7",
"P":"1006",
"S":"14",
"T":"8.0",
"V":"12000",
"W":"7",
"Pt":"F",
"Dp":"6.9",
"$":"120"
},
{
"D":"S",
"H":"93.3",
"P":"1005",
"S":"13",
"T":"8.1",
"V":"13000",
"W":"7",
"Pt":"F",
"Dp":"7.1",
"$":"180"
},
{
"D":"S",
"H":"93.4",
"P":"1004",
"S":"13",
"T":"8.2",
"V":"11000",
"W":"7",
"Pt":"F",
"Dp":"7.2",
"$":"240"
},
{
"D":"S",
"H":"94.0",
"P":"1003",
"S":"15",
"T":"8.4",
"V":"9000",
"W":"8",
"Pt":"F",
"Dp":"7.5",
"$":"300"
},
{
"D":"SSW",
"H":"94.0",
"P":"1002",
"S":"17",
"T":"8.4",
"V":"10000",
"W":"8",
"Pt":"F",
"Dp":"7.5",
"$":"360"
},
{
"D":"S",
"H":"94.7",
"P":"1002",
"S":"13",
"T":"8.4",
"V":"9000",
"W":"8",
"Pt":"F",
"Dp":"7.6",
"$":"420"
},
{
"D":"S",
"H":"94.7",
"P":"1001",
"S":"15",
"T":"8.4",
"V":"9000",
"W":"8",
"Pt":"F",
"Dp":"7.6",
"$":"480"
},
{
"D":"S",
"H":"95.4",
"P":"1000",
"S":"16",
"T":"8.3",
"V":"10000",
"W":"8",
"Pt":"F",
"Dp":"7.6",
"$":"540"
},
{
"D":"NW",
"H":"95.3",
"P":"1000",
"S":"14",
"T":"5.7",
"V":"11000",
"W":"8",
"Pt":"F",
"Dp":"5.0",
"$":"600"
},
{
"D":"NW",
"H":"91.9",
"P":"1001",
"S":"17",
"T":"4.7",
"V":"8000",
"W":"14",
"Pt":"R",
"Dp":"3.5",
"$":"660"
},
{
"D":"WNW",
"H":"90.6",
"P":"1001",
"S":"17",
"T":"4.9",
"V":"9000",
"W":"15",
"Pt":"R",
"Dp":"3.5",
"$":"720"
},
{
"D":"NW",
"H":"89.4",
"P":"1001",
"S":"16",
"T":"4.9",
"V":"16000",
"W":"12",
"Pt":"R",
"Dp":"3.3",
"$":"780"
},
{
"D":"NW",
"H":"85.6",
"P":"1002",
"S":"17",
"T":"5.3",
"V":"50000",
"W":"8",
"Pt":"R",
"Dp":"3.1",
"$":"840"
},
{
"D":"NW",
"H":"86.2",
"P":"1003",
"S":"14",
"T":"5.3",
"V":"28000",
"W":"7",
"Pt":"R",
"Dp":"3.2",
"$":"900"
}
]
}
]
},
{
"i":"3005",
"lat":"60.139",
"lon":"-1.183",
"name":"LERWICK (S. SCREEN)",
"country":"SCOTLAND",
"continent":"EUROPE",
"elevation":"82.0",
"Period":[
{
"type":"Day",
"value":"2015-01-05Z",
"Rep":[
{
"D":"S",
"H":"89.5",
"P":"1013",
"S":"11",
"T":"8.0",
"V":"21000",
"W":"7",
"Pt":"F",
"Dp":"6.4",
"$":"900"
},
{
"D":"SW",
"H":"86.0",
"P":"1012",
"S":"21",
"T":"8.7",
"V":"23000",
"W":"7",
"Pt":"F",
"Dp":"6.5",
"$":"960"
},
{
"D":"SW",
"G":"30",
"H":"85.9",
"P":"1012",
"S":"24",
"T":"8.5",
"V":"26000",
"W":"7",
"Pt":"F",
"Dp":"6.3",
"$":"1020"
},
{
"D":"SW",
"G":"32",
"H":"87.8",
"P":"1012",
"S":"25",
"T":"8.3",
"V":"18000",
"W":"7",
"Pt":"F",
"Dp":"6.4",
"$":"1080"
},
{
"D":"S",
"G":"32",
"H":"86.5",
"P":"1011",
"S":"14",
"T":"7.9",
"V":"25000",
"W":"7",
"Pt":"F",
"Dp":"5.8",
"$":"1140"
},
{
"D":"S",
"H":"90.7",
"P":"1011",
"S":"15",
"T":"7.6",
"V":"22000",
"W":"8",
"Pt":"F",
"Dp":"6.2",
"$":"1200"
},
{
"D":"SSW",
"H":"90.2",
"P":"1010",
"S":"15",
"T":"7.8",
"V":"25000",
"W":"8",
"Pt":"F",
"Dp":"6.3",
"$":"1260"
},
{
"D":"SSW",
"H":"83.1",
"P":"1010",
"S":"18",
"T":"8.5",
"V":"24000",
"W":"8",
"Pt":"F",
"Dp":"5.8",
"$":"1320"
},
{
"D":"SSW",
"H":"86.5",
"P":"1009",
"S":"16",
"T":"8.0",
"V":"20000",
"W":"8",
"Pt":"F",
"Dp":"5.9",
"$":"1380"
}
]
},
{
"type":"Day",
"value":"2015-01-06Z",
"Rep":[
{
"D":"SSW",
"H":"87.1",
"P":"1008",
"S":"14",
"T":"8.2",
"V":"20000",
"W":"8",
"Pt":"F",
"Dp":"6.2",
"$":"0"
},
{
"D":"S",
"H":"90.2",
"P":"1007",
"S":"14",
"T":"8.0",
"V":"19000",
"W":"8",
"Pt":"F",
"Dp":"6.5",
"$":"60"
},
{
"D":"S",
"H":"92.0",
"P":"1006",
"S":"15",
"T":"7.9",
"V":"16000",
"W":"8",
"Pt":"F",
"Dp":"6.7",
"$":"120"
},
{
"D":"S",
"H":"92.7",
"P":"1005",
"S":"15",
"T":"7.9",
"V":"15000",
"W":"8",
"Pt":"F",
"Dp":"6.8",
"$":"180"
},
{
"D":"W",
"H":"96.0",
"P":"1005",
"S":"13",
"T":"7.6",
"V":"15000",
"W":"8",
"Pt":"F",
"Dp":"7.0",
"$":"240"
},
{
"D":"S",
"H":"94.7",
"P":"1003",
"S":"18",
"T":"8.0",
"V":"11000",
"W":"8",
"Pt":"F",
"Dp":"7.2",
"$":"300"
},
{
"D":"S",
"H":"94.1",
"P":"1002",
"S":"17",
"T":"8.1",
"V":"14000",
"W":"8",
"Pt":"F",
"Dp":"7.2",
"$":"360"
},
{
"D":"SSW",
"H":"92.7",
"P":"1002",
"S":"19",
"T":"8.0",
"V":"10000",
"W":"9",
"Pt":"F",
"Dp":"6.9",
"$":"420"
},
{
"D":"SSW",
"G":"30",
"H":"95.3",
"P":"1001",
"S":"24",
"T":"7.9",
"V":"12000",
"W":"7",
"Pt":"F",
"Dp":"7.2",
"$":"480"
},
{
"D":"SW",
"G":"40",
"H":"92.7",
"P":"1001",
"S":"24",
"T":"7.4",
"V":"15000",
"W":"9",
"Pt":"F",
"Dp":"6.3",
"$":"540"
},
{
"D":"NW",
"G":"30",
"H":"94.5",
"P":"1001",
"S":"21",
"T":"5.1",
"V":"7000",
"W":"12",
"Pt":"F",
"Dp":"4.3",
"$":"600"
},
{
"D":"NW",
"H":"93.2",
"P":"1002",
"S":"17",
"T":"4.2",
"V":"9000",
"W":"15",
"Pt":"R",
"Dp":"3.2",
"$":"660"
},
{
"D":"WNW",
"H":"91.8",
"P":"1002",
"S":"16",
"T":"4.5",
"V":"15000",
"W":"15",
"Pt":"R",
"Dp":"3.3",
"$":"720"
},
{
"D":"NW",
"H":"89.2",
"P":"1002",
"S":"18",
"T":"4.2",
"V":"20000",
"W":"7",
"Pt":"R",
"Dp":"2.6",
"$":"780"
},
{
"D":"W",
"H":"85.4",
"P":"1003",
"S":"13",
"T":"4.4",
"V":"30000",
"W":"7",
"Pt":"R",
"Dp":"2.2",
"$":"840"
},
{
"D":"WNW",
"H":"79.7",
"P":"1004",
"S":"11",
"T":"4.9",
"V":"30000",
"W":"2",
"Pt":"R",
"Dp":"1.7",
"$":"900"
}
]
}
]
},
{
"i":"3006",
"lat":"60.447",
"lon":"-1.277",
"name":"SELLA NESS",
"country":"SCOTLAND",
"continent":"EUROPE",
"elevation":"7.0",
"Period":[
{
"type":"Day",
"value":"2015-01-05Z",
"Rep":[
{
"D":"SW",
"S":"17",
"$":"900"
},
{
"D":"SW",
"S":"17",
"$":"960"
},
{
"D":"SSW",
"S":"17",
"$":"1020"
},
{
"D":"SSW",
"S":"19",
"$":"1080"
},
{
"D":"SSW",
"S":"22",
"$":"1140"
},
{
"D":"SSW",
"S":"23",
"$":"1200"
},
{
"D":"SSW",
"G":"30",
"S":"23",
"$":"1260"
},
{
"D":"SSW",
"G":"30",
"S":"23",
"$":"1320"
},
{
"D":"SSW",
"G":"29",
"S":"19",
"$":"1380"
}
]
},
{
"type":"Day",
"value":"2015-01-06Z",
"Rep":[
{
"D":"SW",
"S":"19",
"$":"0"
},
{
"D":"SSW",
"G":"29",
"S":"21",
"$":"60"
},
{
"D":"SSW",
"S":"17",
"$":"120"
},
{
"D":"SSW",
"S":"17",
"$":"180"
},
{
"D":"SSW",
"S":"17",
"$":"240"
},
{
"D":"SSW",
"S":"21",
"$":"300"
},
{
"D":"SSW",
"G":"29",
"S":"19",
"$":"360"
},
{
"D":"SSW",
"S":"19",
"$":"420"
},
{
"D":"SSW",
"S":"21",
"$":"480"
},
{
"D":"NW",
"S":"16",
"$":"540"
},
{
"D":"NW",
"S":"19",
"$":"600"
},
{
"D":"WNW",
"S":"16",
"$":"660"
},
{
"D":"WNW",
"S":"17",
"$":"720"
},
{
"D":"NNW",
"G":"29",
"S":"8",
"$":"780"
},
{
"D":"WNW",
"S":"15",
"$":"840"
},
{
"D":"WNW",
"S":"15",
"$":"900"
}
]
}
]
},
{
"i":"3017",
"lat":"58.954",
"lon":"-2.9",
"name":"KIRKWALL",
"country":"SCOTLAND",
"continent":"EUROPE",
"elevation":"26.0",
"Period":[
{
"type":"Day",
"value":"2015-01-05Z",
"Rep":[
{
"D":"SSW",
"H":"89.5",
"P":"1013",
"S":"13",
"T":"7.8",
"V":"19000",
"W":"3",
"Pt":"F",
"Dp":"6.2",
"$":"900"
},
{
"D":"SSW",
"H":"88.3",
"P":"1013",
"S":"11",
"T":"7.8",
"V":"23000",
"W":"7",
"Pt":"F",
"Dp":"6.0",
"$":"960"
},
{
"D":"SSW",
"H":"89.5",
"P":"1013",
"S":"14",
"T":"7.5",
"V":"20000",
"W":"7",
"Pt":"F",
"Dp":"5.9",
"$":"1020"
},
{
"D":"S",
"H":"90.8",
"P":"1012",
"S":"16",
"T":"7.7",
"V":"20000",
"W":"8",
"Pt":"F",
"Dp":"6.3",
"$":"1080"
},
{
"D":"S",
"H":"90.7",
"P":"1011",
"S":"16",
"T":"7.8",
"V":"15000",
"W":"8",
"Pt":"F",
"Dp":"6.4",
"$":"1140"
},
{
"D":"S",
"H":"90.2",
"P":"1011",
"S":"16",
"T":"7.9",
"V":"15000",
"W":"8",
"Pt":"F",
"Dp":"6.4",
"$":"1200"
},
{
"D":"S",
"H":"89.0",
"P":"1010",
"S":"14",
"T":"8.2",
"V":"17000",
"W":"7",
"Pt":"F",
"Dp":"6.5",
"$":"1260"
},
{
"D":"S",
"H":"89.0",
"P":"1010",
"S":"16",
"T":"8.1",
"V":"17000",
"W":"8",
"Pt":"F",
"Dp":"6.4",
"$":"1320"
},
{
"D":"SSW",
"H":"87.7",
"P":"1009",
"S":"15",
"T":"8.0",
"V":"35000",
"W":"7",
"Pt":"F",
"Dp":"6.1",
"$":"1380"
}
]
},
{
"type":"Day",
"value":"2015-01-06Z",
"Rep":[
{
"D":"S",
"H":"90.1",
"P":"1008",
"S":"17",
"T":"7.4",
"V":"19000",
"W":"7",
"Pt":"F",
"Dp":"5.9",
"$":"0"
},
{
"D":"SSW",
"H":"89.6",
"P":"1007",
"S":"15",
"T":"8.0",
"V":"14000",
"W":"7",
"Pt":"F",
"Dp":"6.4",
"$":"60"
},
{
"D":"SSW",
"H":"88.3",
"P":"1006",
"S":"13",
"T":"7.9",
"V":"19000",
"W":"7",
"Pt":"F",
"Dp":"6.1",
"$":"120"
},
{
"D":"S",
"H":"91.5",
"P":"1005",
"S":"16",
"T":"7.9",
"V":"13000",
"W":"2",
"Pt":"F",
"Dp":"6.6",
"$":"180"
},
{
"D":"S",
"H":"92.7",
"P":"1004",
"S":"16",
"T":"7.7",
"V":"13000",
"W":"2",
"Pt":"F",
"Dp":"6.6",
"$":"240"
},
{
"D":"SSW",
"H":"90.8",
"P":"1003",
"S":"17",
"T":"7.9",
"V":"27000",
"W":"2",
"Pt":"F",
"Dp":"6.5",
"$":"300"
},
{
"D":"SSW",
"H":"92.7",
"P":"1002",
"S":"17",
"T":"7.9",
"V":"13000",
"W":"8",
"Pt":"F",
"Dp":"6.8",
"$":"360"
},
{
"D":"NW",
"H":"93.2",
"P":"1003",
"S":"14",
"T":"5.2",
"V":"30000",
"W":"8",
"Pt":"F",
"Dp":"4.2",
"$":"420"
},
{
"D":"WNW",
"H":"92.6",
"P":"1004",
"S":"10",
"T":"4.8",
"V":"10000",
"W":"15",
"Pt":"R",
"Dp":"3.7",
"$":"480"
},
{
"D":"WNW",
"H":"93.9",
"P":"1004",
"S":"9",
"T":"4.7",
"V":"8000",
"W":"12",
"Pt":"R",
"Dp":"3.8",
"$":"540"
},
{
"D":"WNW",
"H":"91.3",
"P":"1004",
"S":"10",
"T":"4.8",
"V":"16000",
"W":"12",
"Pt":"R",
"Dp":"3.5",
"$":"600"
},
{
"D":"NW",
"H":"83.3",
"P":"1005",
"S":"15",
"T":"4.8",
"V":"50000",
"W":"12",
"Pt":"R",
"Dp":"2.2",
"$":"660"
},
{
"D":"NW",
"H":"86.8",
"P":"1005",
"S":"9",
"T":"4.9",
"V":"7000",
"W":"10",
"Pt":"R",
"Dp":"2.9",
"$":"720"
},
{
"D":"NNW",
"H":"91.2",
"P":"1006",
"S":"13",
"T":"3.7",
"V":"8000",
"W":"11",
"Pt":"R",
"Dp":"2.4",
"$":"780"
},
{
"D":"W",
"H":"89.3",
"P":"1006",
"S":"5",
"T":"3.8",
"V":"35000",
"W":"7",
"Pt":"R",
"Dp":"2.2",
"$":"840"
},
{
"D":"W",
"H":"89.4",
"P":"1007",
"S":"15",
"T":"4.9",
"V":"18000",
"W":"10",
"Pt":"R",
"Dp":"3.3",
"$":"900"
}
]
}
]
}
]
}
}
}
ASKER
Traceback (most recent call last):
File "C:\Temp\MetOfficehourlyjs
rep = location['Period']['Rep']
TypeError: list indices must be integers, not str
Open in new window
I also need to append the data from Period>type and value to the csv. I modified the code but it doesn't like to append both data and pdata:
Open in new window
Traceback (most recent call last):
File "N:\Configuration\MetOffic
writer.writerow(d)
File "C:\Python34\lib\csv.py", line 153, in writerow
return self.writer.writerow(self.
File "C:\Python34\lib\csv.py", line 149, in _dict_to_list
+ ", ".join([repr(x) for x in wrong_fields]))
ValueError: dict contains fields not in fieldnames: 'data_type', 'data_date'
Open in new window