So I am parsing some JSON file and it always breaks when code gets to one that has empty '' value for .items():
for i,n in v['objects'].items():
I get:
AttributeError
'str' object has no attribute 'items'
Whenever code gets to some item that has field:
"objects": ""
Does anyone know how to handle this? I tried with checking first if it is empty, but no success.
I tried with this before my FOR:
objects_empty = ""
if not v['objects'].items() == objects_empty:
Thanks!
[From the earlier comment]
v['objects'] is the value that can be an empty string instead of a dictionary. Hence, you want to check whether this value equals the empty string and not its .items(). A string doesn't have items, that's what the error message complains about.
You can change it to this:
if v["objects"] != "":
...
Related
I am trying to take the value from the input and put it into the browser.find_elements_by_xpath("//div[#class='v1Nh3 kIKUG _bz0w']") function. However, the string formatting surely doesn't work, since it's the list, hence it throws the AttributeError.
Does anyone know any alternatives to use with lists (possibly without iterating over each file)?
xpath_to_links = input('Enter the xpath to links: ')
posts = browser.find_elements_by_xpath("//div[#class='{}']").format(devops)
AttributeError: 'list' object has no attribute 'format'
Looks like the reason of error is that you are placing the format function in the wrong place, so instead of operating on string "//div[#class='{}']" you call it for the list returned by find_elements_by_xpath. Could you please try to replace your code with one of the following lines ?
posts = browser.find_elements_by_xpath("//div[#class='{}']".format(devops))
posts = browser.find_elements_by_xpath(f"//div[#class='{devops}']")
I have trouble making my code work. I post only relevant part of the code.
File im using is in this page https://programmeerimine.cs.ut.ee/_downloads/lapsed.txt
First number is parent and 2nd his child. I also had different filed which translated numbers into name. (I made list ID_name it works fine i checked)
This other part of the code works fine except when I'm trying to add value to existing key.I get error AttributeError: 'str' object has no attribute 'append'
for line in f:
part=line.split(" ")
parent=part[0]
kid=part[1].strip()
for el in ID_name:
if parent == el[0]:
parent=el[1]
if kid == el[0]:
kid=el[1]
if parent not in parents.keys():
parents[parent]=kid
else:
parents[parent].append(kid)
The append function you're referencing only works for lists: https://docs.python.org/2/tutorial/datastructures.html
If you want to add a new key/value pair to a dictionary, use: dictionary['key'] = value .
You can also opt for: dictionary.update({'key': value}), which works well for adding multiple key/value pairs at once.
You need to initialize a list rather than just adding the object. Change this:
parents[parent]=kid
to this:
parents[parent] = [kid]
This will give you a list to which you can append() new objects, rather than just a string.
I try to get the value of a key in a dict by :
print User['LocationName']
This is giving me a TypeError: string indices must be integers, not str error.
This is the dict
{u'LocationName': u'home', u'First Name': u'Bob',...... }
I'm not sure what this error means
User is not a dict. User is a string. Figure out why it's a string, and change that.
The error means that you are trying to access a "part" of your string as if it was a list. But you are not using a integer as parameter to access a list, you are using a string.
Try to use:
eval(User)
then you can:
print User['LocationName']
This usually happens when you save a JSON into a file, using python. It's like Python saved your dict as a dict Object, but when you read from the file, the object is read by string. So, when you call eval(), you get a dict again.
I have two dictionaries:
One is :
data_obt={'Sim_1':{'sig1':[1,2,3],'sig2':[4,5,6]},'Sim_2':{'sig3':[7,8,9],'sig4':[10,11,12]},'Com_1':{'sig5':[13,14,15],'sig6':[16,17,18]},'Com_2':{'sig7':[19,20,21],'sig9':[128,23,24]}}
Other one is:
simdict={'sig1':'Bit 1','sig2':'Bit 2','sig3':'Bit 3','sig4':'Bit 4','sig5':'Bit 5','sig6':'Bit 6','sig7':'Bit 7','sig9':''}
Now I have to do return_data[fpath].append(data_obt[key][item]), where fpath = 'sig9',key='Com_2' and item = 'sig9'
But when I tried to execute this it is throwing error like : KeyError: 'sig9'
My expected return_data is {'sig9':[128,23,24]}
Can anyone please help me out?
As I understand, return_data is another dict. If so, it doesn't (as of yet) have a key named fpath (which is 'sig9'). Hence, the error.
To avoid it, you should either use defaultdict, or initialize this element as an empty list every time you come across a new key.
import json
import simplejson
import urllib2
data = urllib2.urlopen('www.example.com/url/where/i/get/json/data').read()
j = ""
j = simplejson.loads(data)
dump_data=simplejson.dumps(j)
for data in j["facets"]:
print data.items()
print "\n----------------\n"
The error message says it all. Clearly j["facets"] is an iterable which contains at least some strings instead of containing some other datatype which has an items method. (maybe you expected a dict)?
Try printing j["facets"] to see what you're actually getting there. Then you might be able to figure out why you're getting a string instead of the expected object (dict).
Title contains the answer
j["facets"] is probably a list of string items