I'm trying to loop through this json string
"layoutOptions": {
"titleText": "Route Number",
"authorText": "West LA Yard",
"copyrightText": "",
"customTextElements": [{
"Date": "9/11/2018, 7:37:35 AM"
}
],
"scaleBarOptions": {
"metricUnit": "esriKilometers",
"metricLabel": "km",
"nonMetricUnit": "esriMiles",
"nonMetricLabel": "mi"
},
"legendOptions": {
"operationalLayers": [{
"id": "ParcelRouteEditingTest_1458"
}, {
"id": "ParcelRouteEditingTest_1259"
}
]
}
}
I keep running to this error list indices must be integers, not str
layoutOpsDict = layoutData["layoutOptions"]
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
Error:
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
TypeError: string indices must be integers, not str
What is the best method to grab the date in customTextElements other than keep setting more variables to keep track of?
You are looping through every key instead of just "customTextElements" and not all of them have a list of dictionaries with "Date" as the key.
Since you only want to look through the values mapped to "customTextElements" you can only loop through that:
dateList = [dateEle["Date"] for dateEle in layoutOpsDict["customTextElements"]]
Related
I am trying to duplicate a simple code in my reading material where I want to extract data from a JSON file and plot dots at the capitals of countries on a map.
Regarding my issue,
Traceback (most recent call last):
File "C:\Users\serta\Desktop\python\db\capitals.py", line 14, in <module>
lons.append(cp_dicts['geometries']['coordinates'][0])
TypeError: string indices must be integers
[Finished in 188ms]
I read similar posts here and I think I understand the "why" of the issue,I double checked my []'s and the depths of the nests but I cannot seem to fix it myself.
I am pretty sure I am looking at an integer where I target with my code (lons.append line) but I am still getting the "TypeError: string indices must be integers".
Here is the code:
import json
from plotly.graph_objs import Scattergeo, Layout
from plotly import offline
#Explore the structure of the data.
filename = 'data/capitals.topo.json'
with open (filename) as f:
all_cp_data = json.load(f)
all_cp_dicts=all_cp_data
lons,lats,hover_texts =[],[],[]
for cp_dicts in all_cp_dicts['objects']['capitals']:
lons.append(cp_dicts['geometries']['coordinates'][0])
lats.append(cp_dicts['geometries']['coordinates'][1])
hover_texts.append(cp_dicts['properties']['capital'])
#Map the earthquakes.
data = [{
'type':'scattergeo',
'lon':lons,
'lat':lats,
'text': hover_texts,
'marker':{
'size': [5],
#'color': mags,
#'colorscale': 'plasma',
#'reversescale':True,
#'colorbar':{'title':'Magnitude'},
},
}]
my_layout = Layout(title="Capital Cities")
fig ={'data':data, 'layout':my_layout}
offline.plot(fig, filename ='capital_cities.html')
Here is also the capitals.topo.json I am using:
{
"type": "Topology",
"objects": {
"capitals": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [
90.24,
23.43
],
"id": "BD",
"properties": {
"country": "Bangladesh",
"city": "Dhaka",
"tld": "bd",
"iso3": "BGD",
"iso2": "BD"
}
},
On line 14, this is not valid, given the input data:
lons.append(cp_dicts['geometries']['coordinates'][0]
You need to update the loop along these lines:
for geometry in all_cp_dicts['objects']['capitals']['geometries']:
lons.append(geometry['coordinates'][0])
lats.append(geometry['coordinates'][1])
hover_texts.append(geometry['properties'].get('city', ""))
Note that for some of the locations, the 'cities' key is missing in the json. So you need to handle that when populating the hover_texts list, as shown.
Also, the 'data' variable was not working with Scattergeo. Below is a suggested revision to the syntax
data = Scattergeo(
lat = lats,
lon = lons,
text = hover_texts,
marker = dict(
size = 5
)
)
Trying to run code where I create empty list by iterating events from json and append userid of any user who did homepage event into the empty list. I'm trying to use conditional parsing to only parse out 'name' key value with pair 'Visited home page' from the events.json
My code is as follows:
import json
#step 1 -- read events file into string
with open('events.json') as events_data:
#step 2 -- convert string to json object (json.load(string)
events_data = json.load(events_data)
#print (events_data['events'][0]['name'])
#print(events_data.keys())
#with open ("data/events.json") as events_file:
#step 3 -- create empty list; iterate events list and append userid of any user who did homepage event into the empty list
#that gives us set of user ids who did homepage event
homeuserid = []
for i in events_data['events']:
homeuserid[i] = i ['name'] ['Visited home page']
print(homeuserid)
However when I go the run the code I get the following error and I am unsure why:
Traceback (most recent call last):
File "run.py", line 34, in <module>
homeuserid[i] = i ['name'] ['Visited home page']
TypeError: string indices must be integers
Sample of JSON (events.json):
{
"events": [
{
"name": "Added item to cart",
"timestamp": 1422119921,
"user_id": "5a5598f2-f7db-420e-9b8e-52a9ad694bc1"
},
{
"name": "Visited product page",
"timestamp": 1409554014,
"user_id": "4683c9b6-3c8b-4215-a401-a9bbfde833ee"
},
{
"name": "Visited about page",
"timestamp": 1430938313,
"user_id": "26a1593b-b17d-4389-aa93-4a1c9c0e9c67"
},
{
"name": "Added item to cart",
"timestamp": 1427447392,
"user_id": "e71f2ee8-09ce-412b-92e1-3c6c0a90dda8"
},
Here's the proper way to do it, there's no reason to process the file twice. Note that the resulting list may be empty if there are no 'Visited home page' events.
import json
filepath = 'events.json'
with open(filepath) as events_file:
events_data = json.load(events_file)
homeuserids = []
for event in events_data['events']:
if event['name'] == 'Visited home page':
homeuserids.append(event['user_id'])
print(homeuserids)
I am parsing a JSON file with Python. One of they keys I am trying to parse has a float value, and I am getting the following error: TypeError: list indices must be integers, not str. Below is the code, JSON, and full traceback.
Code:
import json
with open('output.json') as f:
data = json.load(f)
for host in data['ASSET_DATA_REPORT']['HOST_LIST']['HOST']:
print(host['VULN_INFO_LIST']['VULN_INFO']['CVSS_FINAL'])
JSON:
{
"ASSET_DATA_REPORT":{
"HOST_LIST":{
"HOST":[
{
"IP":{
"network_id":"0"
},
"TRACKING_METHOD":"EC2",
"ASSET_TAGS":{
"ASSET_TAG":[
"EC2 Running",
"IF - Database - MySQL"
]
},
"DNS":"i-xxxxxxx",
"EC2_INSTANCE_ID":"i-xxxxxx",
"EC2_INFO":{
"PUBLIC_DNS_NAME":"ec2-xxxxxxxx.amazonaws.com",
"IMAGE_ID":"ami-xxxxxx",
"VPC_ID":"vpc-xxxxxx",
"INSTANCE_STATE":"RUNNING",
"PRIVATE_DNS_NAME":"ip-xxxx.ec2.internal",
"INSTANCE_TYPE":"m3.xlarge"
},
"VULN_INFO_LIST":{
"VULN_INFO":[
{
"CVSS_FINAL":"3.6"
}
]
}
}
]
}
}
}
Traceback:
Traceback (most recent call last):
File "json_format.py", line 11, in <module>
print(host['VULN_INFO_LIST']['VULN_INFO']['CVSS_FINAL'])
TypeError: list indices must be integers, not str
The dictionary containing the "CVSS_FINAL" key is actually itself in a list. Try:
print(host['VULN_INFO_LIST']['VULN_INFO'][0]['CVSS_FINAL'])
As an aside, if you want to store this value as type float in Python (rather than string), you could do:
value = float(host['VULN_INFO_LIST']['VULN_INFO'][0]['CVSS_FINAL'])
The issue I'm having, I cant make the aggregate search work. I have been able to run it in a mongodb without any users, however in the mongodb with users (even when authentication is not turned on) i get this weird error message (see below). Whats weirder is I have no problem with other functions for example find().count() witch as you can see below returns 32 (the number of results in the database).
Code in MongoDB shell to create user.
use admin
db.createUser( { user: "VibrationDataCollector",
pwd: '45.',
roles: [ "readWriteAnyDatabase",
"dbAdminAnyDatabase",
"clusterAdmin" ] } )
Code in python to conduct search
client = MongoClient('mongodb://xx.x.x.xx:xxxxx/')
db = client['VibrationDataDB']
db.authenticate('VibrationDataCollector', '45.', source='admin')
coll = db.VibrationData
hello = coll.find().count()
print 'count=', hello
LastWrite_Time = coll.aggregate([{
"$unwind": "$Records"
}, {
"$redact": {
"$cond": [{
"$anyElementTrue": {
"$map": {
"input": "$Records.Properties",
"as": "result",
"in": {
"$and": [{
"$eq": ["$$result.Property.Name", "LastWrite_User"]
}, {
"$eq": ["$$result.value", "42"]
}]
}
}
}
},
"$$KEEP",
"$$PRUNE"
]
}
}, {
"$unwind": "$Records.Properties"
}, {
"$match": {
"Records.Properties.Property.Name": 'LastWrite_Time'
}
}, {
"$project": {
"_id": 0,
"value": "$Records.Properties.value"
}
}])
list1 = LastWrite_Time['result']
for T in list1:
print T['value']
Result
count= 32
Traceback (most recent call last):
File "C:/Python_scripts/SimulationCEI.py", line 64, in <module>
list1 = LastWrite_Time['result']
TypeError: 'CommandCursor' object has no attribute '__getitem__'
UPDATEEEEE!!!!
using next()
count= 32
Traceback (most recent call last):
File "C:/Python_scripts/SimulationCEI.py", line 64, in <module>
list1 = LastWrite_Time['result']
TypeError: 'CommandCursor' object has no attribute '__getitem__'
>>> next()
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
next()
TypeError: next expected at least 1 arguments, got 0
>>>
You want:
for T in LastWrite_Time:
print T['value']
"aggregate" returns a cursor that must be iterated. I'm certain that authentication is not related to the error you see.
I have to following bit of JSON data which is a snippet from a large file of JSON.
I'm basically just looking to expand this data.
I'll worry about adding it to the existing JSON file later.
The JSON data snippet is:
"Roles": [
{
"Role": "STACiWS_B",
"Settings": {
"HostType": "AsfManaged",
"Hostname": "JTTstSTBWS-0001",
"TemplateName": "W2K16_BETA_4CPU",
"Hypervisor": "sys2Director-pool4",
"InCloud": false
}
}
],
So what I want to do is to make many more datasets of "role" (for lack of a better term)
So something like this:
"Roles": [
{
"Role": "Clients",
"Settings": {
"HostType": "AsfManaged",
"Hostname": "JTClients-0001",
"TemplateName": "Win10_RTM_64_EN_1511",
"Hypervisor": "sys2director-pool3",
"InCloud": false
}
},
{
"Role": "Clients",
"Settings": {
"HostType": "AsfManaged",
"Hostname": "JTClients-0002",
"TemplateName": "Win10_RTM_64_EN_1511",
"Hypervisor": "sys2director-pool3",
"InCloud": false
}
},
I started with some python code that looks like so, but, it seems I'm fairly far off the mark
import json
import pprint
Roles = ["STACiTS","STACiWS","STACiWS_B"]
RoleData = dict()
RoleData['Role'] = dict()
RoleData['Role']['Setttings'] = dict()
ASFHostType = "AsfManaged"
ASFBaseHostname = ["JTSTACiTS","JTSTACiWS","JTSTACiWS_"]
HypTemplateName = "W2K12R2_4CPU"
HypPoolName = "sys2director"
def CreateASF_Roles(Roles):
for SingleRole in Roles:
print SingleRole #debug purposes
if SingleRole == 'STACiTS':
print ("We found STACiTS!!!") #debug purposes
NumOfHosts = 1
for NumOfHosts in range(20): #Hardcoded for STACiTS - Generate 20 STACiTS datasets
RoleData['Role']=SingleRole
RoleData['Role']['Settings']['HostType']=ASFHostType
ASFHostname = ASFBaseHostname + '-' + NumOfHosts.zfill(4)
RoleData['Role']['Settings']['Hostname']=ASFHostname
RoleData['Role']['Settings']['TemplateName']=HypTemplateName
RoleData['Role']['Settings']['Hypervisor']=HypPoolName
RoleData['Role']['Settings']['InCloud']="false"
CreateASF_Roles(Roles)
pprint.pprint(RoleData)
I keep getting this error, which is confusing, because I thought dictionaries could have named indices.
Traceback (most recent call last):
File ".\CreateASFRoles.py", line 34, in <module>
CreateASF_Roles(Roles)
File ".\CreateASFRoles.py", line 26, in CreateASF_Roles
RoleData['Role']['Settings']['HostType']=ASFHostType
TypeError: string indices must be integers, not str
Any thoughts are appreciated. thanks.
Right here:
RoleData['Role']=SingleRole
You set RoleData to be the string 'STACiTS'. So then the next command evaluates to:
'STACiTS'['Settings']['HostType']=ASFHostType
Which of course is trying to index into a string with another string, which is your error. Dictionaries can have named indices, but you overwrote the dictionary you created with a string.
You likely intended to create RoleData["Settings"] as a dictionary then assign to that, rather than RoleData["Role"]["Settings"]
Also on another note, you have another syntax error up here:
RoleData['Role']['Setttings'] = dict()
With a mispelling of "settings" that will probably cause similar problems for you later on unless fixed.