I have a dictionary with a single item in it created using json.loads(). The data structure looks like this:
{"teamId":96}
When I attempt to access the dictionary value by using the following:
mydict = mydict[u'teamId']
I get the following error:
Traceback (most recent call last):
File "C:\Python27\counter.py", line 65, in <module>
print home_team[u'teamId']
TypeError: string indices must be integers
Can anyone explain to me what the issue is here? The code looks like it should work to me.
Thanks
You need json.dumps(you_file) :
json.loads(json.dumps(your_file))
Related
I'm trying to program a reddit bot, but get this error message:
Traceback (most recent call last):
File "main.py", line 64, in <module>
run_bot(r, comments_replied_to)
File "main.py", line 34, in run_bot
comments_replied_to.append(comment.id)
AttributeError: 'filter' object has no attribute 'append'
Here is my code: https://pastebin.com/caz14jm7
I think I have to change append into a list, but I don't know how to do that
Part of this is a version difference. In Python 2, filter returned a list. In Python 3, filter returns a generator, which you are treating like a list. The filter generator does not have an append method.
So, you just need to turn it into a list:
comments_replied_to = list(filter(None, comments_replied_to))
Or, even better (in the opinion of many):
comments_replied_to = [k for k in comments_replied_to if k]
I have 2 potential solutions for you,
solution 1:
Line 55 You can change like this
comments_replied_to = List(filter(None, comments_replied_to))
so that, your get_saved_comments() will return a list.
This will help you use the append method and comments_replied_to.append(comment.id) should work without any error
Solution 2:
Line 60: You can change like this
comments_replied_to = list(get_saved_comments())
As long as comments_replied_to is a list type, it will allow you to append method without any issues.
check this blog to have a better understanding over append method
https://www.programiz.com/python-programming/methods/list/append
I got two errors in my python Codes. It is a simple Intelligent Voice Assistant. Could you please help me to resolve these problems?
Codes are in the images below:
Image 1, Line 109
Image 2, Line 67
It's hard to answer without more information, but it appears that self.intents is a string instead of a dictionary as expected. Therefore, searching for the key 'intents', you are indexing a string with the index 'intents' when a string index should be an integer.
Having an error where i'm trying to get the first work from a string that is passed in to a method within a class. But i am getting AttributeError: 'Deck' object has no attribute 'split' when I run. The 'new_card' that is passed in will be for example 'Two of Hearts'. and new_Card is a string and self.values is a dictionary
# returns integer value of a card
def get_card_value(self, new_card):
return self.values[new_card.split()[0]]
and the error:
Traceback (most recent call last):
File "/home/andypaling/Documents/Programming/python/random/card_game/game.py", line 146, in
if not Game.check_same_cards(player1_deck, player2_card):
File "/home/andypaling/Documents/Programming/python/random/card_game/game.py", line 87, in check_same_cards
if card1.get_card_value(card1) == card2.get_card_value(card2):
File "/home/andypaling/Documents/Programming/python/random/card_game/game.py", line 40, in get_card_value
split_string = new_card.split(' ')
thanks for any help
Hey it seems like you are using a diffrent data type and not a string in your case judging that your making a card game i am guessing you are using a tuple. Try converting the data to a string then split it using the .split() function.
I hope this can help.
I'm trying to get a key from a value that I am returned with, but when i use the easy approach to just get the value of a specific key I get the error: TypeError: string indices must be integers, not str
I also tried .get() method, but it did not work either. Could someone please point me out what I am doing wrong?
>>> import urllib2
>>> url = 'http://192.168.250.1/ajax.app?SessionId=8ef05397-ef00-451a-bc1c-c0d61
5a4811d&service=getDp&plantItemId=1413'
>>> response = urllib2.urlopen(url)
>>> dict = response.read()
>>> dict
'{"service":"getDp","plantItemId":"1413","value":" 21.4","unit":"\xc2\xb0C"}'
>>> dict['value']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str
Looks like 'dict' is a variable of type string, not a dictionary. You should parse the string into a suitable dictionary format (like JSON). Here's a code that will resolve that issue:
import json
json_string = response.read()
dict = json.loads(json_string)
Now, for dict['value'] you will get what you need.
response.read() is returning an object of type string and not a dictionary and you can index a string using only integer indices and hence you are getting your error.
You need to parse this string and convert it to a dictionary. To convert a string of a dictionary back to a dictionary you can do this:
import ast
dict = ast.literal_eval(dict)
print dict['value']
Tried it on my machine with Python 2.7 and it works.
I am getting following error while updating a document inside a collection in mongodb using python using pymongo. Any help is greatly appreciated.
x = 4
str = "ratings.${x}.rating"
db.amitava1.update({"_id":1},{"$inc":{[str]:1 } } )
Traceback (most recent call last):
File "", line 1, in TypeError: unhashable type:
'list'***
Youre getting that error because you're doing {"$inc":{[str]:1 }}. Namely, trying to assing [str] as the key in the in the dictionary {[str]:1 }.
It says that because you cannot use a list as a key for a dictionary, because a list is unhashable. You can only use hashable types (types that have a __hash__ function defined) key values.
It looks like you have some other issues with your code though. I think you need to use
str = "ratings.${x}.rating".format(x=x)
or something in order to replace the x in your string.