Im currently using the module element tree & urllib to access/parse and return values from xml files.
Using the root.find/root.findall() methods along with XPath syntax to locate desired info in xml. Then using (.text) to return the value of child/grandchild elements.
When assigning each root.find() to a variable then taking the variable and attaching (.text), i am able to parse and return the value with no issues.
(i.e)
x= root.find(./Cameras/Camera/Connected')
print (x.text)
==> True
However I would like to place these "root.find()" in a dictionary and call on them later in the script.
(i.e)
location= {
'Cam': "root.find('./Cameras/Camera/Connected')",
'Mic': "root.findall('./Audio/Input/Connectors/Microphone')",
'Prod_ID': "root.find('./SystemUnit/ProductPlatform')"
}
However, when indexing to dictionary by key and then attempting the add the (.text) i get the following error;
y=location['Cam']
print (y.text)
==> AttributeError: 'str' object has no attribute 'text'
So this maybe a simple issue im overlooking but do these two methods return the same value? Can the element tree module read root.find() from dictionaries?
You're setting your dictionary values to a literal string. That is, this:
'Cam': "root.find('./Cameras/Camera/Connected')",
Is setting the value of key Cam to the string value root.find('./Cameras/Camera/Connected'). You want to actually call the function and set the key to the return value, so you need to drop the quotes:
'Cam': root.find('./Cameras/Camera/Connected'),
Related
I am trying to get the value from a dictionary with a filepath as the key and a number as the value. I am trying to get the value using the dict[key] method and when I configure the variable as either single backslash or double backslash I still get a KeyError when the variables look the same:
from pathlib import Path
print(job_slurm_dict)
print(Path(f'{site_dir}/hfoutp.in'))
try:
print(job_slurm_dict[Path(f'{site_dir}/hfoutp.in')])
except KeyError:
print('KEYERROR 1')
try:
print('\\\\'.join(f'{site_dir}\hfoutp.in'.split('\\')))
print(job_slurm_dict['\\\\'.join(f'{site_dir}\hfoutp.in'.split('\\'))])
except KeyError:
print('KEYERROR 2')
sys.exit(1)
I get the following output:
{'7ded3d66-8ed6-4edc-9127-b6ba2a598369\\reagent\\hfoutp.in': '17312073_1', '7ded3d66-8ed6-4edc-9127-b6ba2a598369\\3\\hfoutp.in': '17312073_2', '7ded3d66-8ed6-4edc-9127-b6ba2a598369\\4\\hfoutp.in': '17312073_3', '7ded3d66-8ed6-4edc-9127-b6ba2a598369\\6\\hfoutp.in': '17312073_4'}
7ded3d66-8ed6-4edc-9127-b6ba2a598369\3\hfoutp.in
KEYERROR 1
7ded3d66-8ed6-4edc-9127-b6ba2a598369\\3\\hfoutp.in
KEYERROR 2
When I try to print each key from the dictionary it looks like this: 7ded3d66-8ed6-4edc-9127-b6ba2a598369\3\hfoutp.in.
I am not sure why I am getting a KeyError when using this variable in either form to access the value from the dictionary since both the key and the variable are exactly the same.
Many thanks in advance.
The problem is that you are trying to print and access the dictionary with different data types.
In the first instance you are accessing the dictionary using a Path and in the second instance using a string. The reason it looks like the same key is because dictionary and Path converts differently to string. A string in a dictionary prints with double \\, while if you print the string directly it will just be one \.
If you convert the Path to string first, i.e., str(Path(...)), then it should work.
I have a JSON output like this:
{
":output":{
"response":"{ \"ParentId\" : 125, \"ParentKey\" : { \"key\" : \"9aqews-uwdguwdw8-9uw8\", \"identity\" : \"key_ID=674\" } }"
}
}
I'm trying to fetch the content of key, that is: 9aqews-uwdguwdw8-9uw8
Here are somethings that tried:
------------------------------------------------------
${json_data} Parse Json ${output}
Log ${json_data["output"]["response"]}
Log ${json_data["output"]["response"][0][0:10]}
------------------------------------------------------
${json}= Convert To Dictionary ${values}
${j_keys}= Get Dictionary Keys ${json}
Log ${j_keys}
------------------------------------------------------
${values}= Evaluate json.loads($output) json
Log ${values['output']['response'][1]}
-----------------------------------------------------
${KeySP}= Get Substring ${values} "key" ","
Log ${keySP}
------------------------------------------------------
#${parkeydict}= ${values['output']['response']}
#${keyspacedict}= ${parkeydict['ParentKey']}
#Log ${keyspacedict['key']}
------------------------------------------------------
I have tried several other steps, possibilities and keywords,
The best I could parse is till "Log ${json_data["output"]["response"]}" which returns data from 'response'.
It fails even if I convert to Dict and access the 'key', I think that further data after 'response' is completely stored as values.
Can someone help/guide me on how to capture the data in 'key' variable?
Thanks in Advance!
The first problem is that you're trying to use ['output'], but the key is :output.
The second problem is that the value of the "response" key is not a dictionary, it's a string that looks like a dictionary. You must convert it to a dictionary before you try to pull values from it, assuming it is indeed an well-formed json dictionary and not just a string that might look like a dictionary.
This works for me on the exact data provided in the question:
${values}= Evaluate json.loads($output)
${response}= Evaluate json.loads($values[':output']['response'])
${key}= set variable ${response['ParentKey']['key']}
should be equal ${key} 9aqews-uwdguwdw8-9uw8
Note: if you're using a version of robot that is older than 3.2 you'll need to include json as a final argument for the Evaluate command so that robot knows to import the module. Starting with version 3.2 and onward, modules used in the expression are automatically imported.
I have this JSON object and i like to update a value within the object. I found a way how i should do this on stackoverflow (Json handling in ROBOT) and its failing and i don't understand why.
This is de object:
{"elementKey":"P690-C0-C3-B1","fields":[{"key":"P690-C1-C2-C1-C1-C1-F0","values":[]},{"key":"P690-C0-C2-F8","values":["1200"]},{"key":"P690-C0-C2-F9","values":["22000"]},{"key":"P690-C0-C2-F11","values":["I"]},{"key":"P690-C0-C2-F10","values":["2200"]},{"key":"P690-C0-C2-C0-C0-F0","values":["98-zsg-2"]},{"key":"P690-C1-C0-C0-F1","values":["Personenauto"]},{"key":"P690-C1-C0-C0-F2","values":["Personenauto KVP"]},{"key":"P690-C0-C2-F6","values":["B"]},{"key":"P690-C0-C2-F7","values":["75"]},{"key":"P690-C0-C2-F4","values":["2"]},{"key":"P690-C0-C2-F5","values":["5"]},{"key":"P690-C0-C2-F2","values":["model"]},{"key":"P690-C0-C2-F3","values":["2017"]},{"key":"P690-C1-C2-C2-C2-C1-F0","values":[]},{"key":"P690-C0-C2-F1","values":["merk"]}]}
In Robot frame I made this test, inspired on the given link.
${json_string}= Set Variable "see text above"
${json}= Evaluate json.loads('''${json_string}''') json
Set To Dictionary ${json["fields"]} ${new_value}
${json_string}= evaluate json.dumps(${json}) json
With ${new_value} i tried value=shizzleliz, value[0]=shizzleliz, value[1]=shizzleliz, P690-C1-C2-C1-C1-C1-F0=shizzleliz
All give the error: AttributeError: 'list' object has no attribute 'update'
When i change ${json["fields"]} to ${json} then the give value is set to the library but not in de fields section/collection.
Does anyone have a clue of what i'm doing wrong? And if you have a suggestion how i can update the value, i'd like that very much :)
target is to change: {"key":"P690-C1-C2-C1-C1-C1-F0","values":[]}
to: {"key":"P690-C1-C2-C1-C1-C1-F0","values":["shizzleliz"]}
For the first part in your question - the error AttributeError: 'list' object has no attribute 'update', you've already seen the comment - you're calling Set To Dictionary on a list object, which cannot pass.
For the second part, in order to set that value when the key is equal to something, you have to iterate over all the list members, and set it based on a condition over the key:
${json_string}= Set Variable see text above
${json1}= Evaluate json.loads('''${json_string}''') json
${target value}= Create List shizzleiz
:FOR ${element} IN #{json1["fields"]}
\ Run Keyword If "${element['key']}" == "P690-C1-C2-C1-C1-C1-F0"
... Set To Dictionary ${element} values=${target value}
${json_string}= evaluate json.dumps(${json1}) json
It looks a little cumbersome in RF (compared to python); one remark - it's never a good idea to name a local variable the same as a module - thus I've renamed it to ${json1}
I found an easier solution using Catenate where I needed to randomize two values in the json body.
${shizzleiz}= shizzleiz # or whatever you want to appear there
${json_string}= Catenate {"elementKey":"P690-C0-C3-B1","fields":[{"key":"P690-C1-C2-C1-C1-C1-F0","values":[]},{"key":"P690-C0-C2-F8","values":["1200"]},{"key":"P690-C0-C2-F9","values":["22000"]},{"key":"P690-C0-C2-F11","values":["I"]},{"key":"P690-C0-C2-F10","values":["2200"]},{"key":"P690-C0-C2-C0-C0-F0","values":["98-zsg-2"]},{"key":"P690-C1-C0-C0-F1","values":["Personenauto"]},{"key":"P690-C1-C0-C0-F2","values":["Personenauto KVP"]},{"key":"P690-C0-C2-F6","values":["B"]},{"key":"P690-C0-C2-F7","values":["75"]},{"key":"P690-C0-C2-F4","values":["2"]},{"key":"P690-C0-C2-F5","values":["5"]},{"key":"P690-C0-C2-F2","values":["model"]},{"key":"P690-C0-C2-F3","values":["2017"]},{"key":"P690-C1-C2-C2-C2-C1-F0","values": ${shizzleiz} ${the-rest-of-the-long-json-as-a-string}
then continue on with:
${json}= Evaluate json.loads('''${json_string}''') json
${json_string}= evaluate json.dumps(${json}) json
(basically do the work before reacting to the json function - obviously this requires knowing the values beforehand and could also work with more variables.)
Use Case
I am making a factory type script in Python that consumes XML and based on that XML, returns information from a specific factory. I have created a file that I call FactoryMap.json that stores the mapping between the location an item can be found in XML and the appropriate factory.
Issue
The JSON in my mapping file looks like:
{
"path": "['project']['builders']['hudson.tasks.Shell']",
"class": "bin.classes.factories.step.ShellStep"
}
path is where the element can be found in the xml once its converted to a dict.
class is the corresponding path to the factory that can consume that elements information.
In order to do anything with this, I need to descend into the dictionaries structure, which would look like this if I didn't have to draw this information from a file(note the key reference = 'path' from my json'):
configDict={my xml config dict}
for k,v in configDict['project']['builders']['hudson.tasks.Shell'].iteritems():
#call the appropriate factory
The issue is that if I look up the path value as a string or a list, I can not use it in 'iteritems'():
path="['project']['builders']['hudson.tasks.Shell']" #this is taken from the JSON
for k,v in configDict[path].iteritems():
#call the appropriate factory
This returns a key error stating that I can't use a string as the key value. How can I used a variable as the key for that python dictionary?
You could use eval:
eval( "configDict"+path )
You can use the eval() function to evaluate your path into an actual dict object vs a string. Something like this is what I'm referring to:
path="['project']['builders']['hudson.tasks.Shell']" #this is taken from the JSON
d = eval("configDict%s" % path)
for k,v in d.iteritems():
#call the appropriate factory
I working with some twitter data, I get the data by monitoring the twitter stream, then I save the results in a *.txt file
I´m trying to manipulate this txt file with python, for that I use the json.loads() instruction, with every line in the file where the twitter stream result was saved, in that way I got every file line as an json object.
for line in twitter_file
data = json.loads(line)
The json object (one json object for one file line) is loaded in a variable called "data"
Everyting is working there, when I try to access an element in the "data" json object I can do it, for example, if I can see the place element, I can do it with data["place"], so I got this:
"place":{
"id":"00a961d91f76dde8",
"url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/00a961d91f76dde8.json",
"place_type":"city",
"name":"Capital - Corrientes",
"full_name":"Capital - Corrientes",
"country_code":"AR",
"country":"Argentina",
"contained_within":[]
}
I can access the "place" element, if I execute print data["place"] , I can see the text displayed above.
The problem comes when I'm trying to access an specific key in place dictionary,
I tried to do it in this way
data["place"]["place_type"]
I'm waiting to get the "city" value as result, but I can not do it, I get the following error: TypeError: "NoneType" object has no attribute '__getitem__'
I also tried other ways to display the key-value pairs with the following:
for key,value in data["data"].items()
print key
But I don't get the result
I also tried with
print data["place"].keys()[0]
To get printed the first element, but It doesn't work either. I got the following error message: AttributeError: 'NoneType' object has no attribute 'keys'
It seems that my data["place"] is not being considered as a dictionary by Python, that's what I guess, but I'm not sure, I'm pretty new at Python, so any comment will be very helpful for me.
You are looping over a file and loading each line as JSON. Some of those JSON objects have place set to a dictionary, others have it set to None.
Whenever you just loaded a line with the value associated with the place key set to None, you'll get the AttributeError exception.
You need to test for this first:
place = data['place']
if place is not None:
print place['place_type']