Python error when doing list comprehension - python

I've got a list of dictionaries with a xpath query from a HTML file.
It is something like this:
[{"not-interested-in":"", "url": url_string1},
{"not-interested-in":"", "url": url_string2}, and so on...]
Now I try to obtain a new list with all the values of the "url" key from all the dictionaries:
[url_string1, url_string2, ...]
I tried list comprehension as it was the most recommended method, and I did this(data is the list of dictionaries):
data = tree.xpath('(//li[#data-bns="API"]/#data)[position() <= 5]')
imageURLs = [di['url'] for di in data]
When running, I got an error message something like this:
TypeError: string indices must be integers, not str
Can anybody help me, please?

In imageURLs = [di['url'] for di in data] you're trying to index into an object (di, which is a string), using another string ('url') as your index. Strings don't support string indexing!
(E.g. 'Hello!'['a'] will return the same error.)

Related

Python reading comma seperated list from JSON

I have the following JSON structure given in a python script:
print("Producers: ", metadata['plist']['dict']['array'][2]['dict']['string'])
The Problem is that I don't have a single entry on that field, instead I have multiple ones.
Please also see the RAW JSON here: https://pastebin.com/rtTgmwvn
How can I pull out these entries as a comma separated string for [2] which is the producers field?
Thanks in advance
You're almost there:
you can do something like this
print("Producers: ", ", ".join(i["string"] for i in metadata['plist']['dict']['array'][2]['dict'])
to break down the solution... your "dict" element in the JSON is actually a list of "dict", and therefore you can simply iterate over this list:
metadata['plist']['dict']['array'][2]['dict']
where each element is an actual dict with a "string" key.
Update
The format of the JSON is so tahat in some cases it is a list, and in some cases it is a single element. In that case, I would suggest writing a small function or use an if statement that handles each situation:
def get_csv(element):
if isinstance(element, dict):
return element["string"]
return ", ".join(i["string"] for i in element)
# and you would use it like so:
print("Producers: ", get_csv(metadata['plist']['dict']['array'][2]['dict']))
The following should do the trick:
def get_producer_csv(data):
producers = []
dict = data["plist"]["dict"]["array"][2]["dict"]
for dict_entry in dict:
producers.append(dict_entry["string"])
return ",".join(producers)
For your example, it returns the following: "David Heyman,David Barron,Tim Lewis"

Removing keys and values from a list of strings

Edit: Sorry for misleading you all, I make amendment to the data. That's what the data looks like after loading it with pandas.
thanks for always helping out. I have a list of strings like this:
Index Data
0 "[{"name": "bob", "age":"11", "id":"94884-0abdvnd-90", "participantid":"Me", "sentiment":"NEUTRAL", "content":"Hey, how you doing."}]"
1 "[{"name": "Roland", "age":"16", "id":"94884-0abdvnd-90", "participantid":"boy", "sentiment":"NEUTRAL", "content":"Hey, I'm doing good and you?."}]"]
And my goal is to remove certain keys and values so I only have the content left. That is:
Index Data
0 "[{"content":"Hey, how you doing."}]"
1 "[{"content":"Hey, I'm doing good and you?."}]"]
My initial approach was to convert each string to list using eval, then loop over it, but that only works for one string at a time. i.e I can only eval on mylist[0] then mylist1 manually.
Here is the screenshot of the data:
Here is the sample of my code:
import ast
x = ast.literal_eval(mylist)
keys_to_keep = ["content"]
new_list = [{ key: item[key] for key in keys_to_keep } for item in x]
The above code will bring an error except I use x[0], x1 etc. Is there any better way of doing this?
Thanks.
You can use json module of Python standard library here - it is safer than using eval on the inputs which might contain code to pretty much anything.
E.g. like this (assuming mylist is a list of strings each of which is a valid json with one-element list):
import json
keys_to_keep = ["content"]
new_list = []
for x in mylist:
item = json.loads(x)
new_list.append({ key: item[key] for key in keys_to_keep})

Slicing a dictionary's value isn't working properly

So I have a json file which contains role ids (a dictionary named roles),with a key (which is the name of the role) and each key has the role's id as it's value
Looks something like this:
{"Ban": ["694175790639743076"], "Muted": ["692863646543380590"]}
I mostly just need the ids from the json
using something like roles['Muted'] or roles.get('Muted') gives me ['692863646543380590']:
Muted=roles.get('Muted')
print(Muted)
the functions take integers,so I have to remove [' from the output and get this: 692863646543380590
slicing just gives me [] no matter what slicing I use:
work=boost1[2:20] *or any other slice*
print(work)
gives out "[]"
why is slicing just not working here? and how do I fix this??
first of all roles['Muted'] is a list, if you want just first element then get it with 0 index then try using int() for converting it to Integer:
Muted=int(roles.get('Muted')[0]) # or Muted=int(roles['Muted'][0])
Muted will be:
692863646543380590
Try this -
work = int(boost1[0]) #fetching the first element of list and converting it into int

Python3 Slicing, TypeError: list indices must be integers or slices, not list

I want to extract information from .json format through API for each element in array.
I tried to use below code but get me an error.
# Get the response from the API endpoint.
response = requests.get("http://api.open-notify.org/astros.json")
data = response.json()
print(data["people"][0:2]["name"])
I would expect to see every name listed instead of an error:
TypeError: list indices must be integers or slices, not list
I know there is error in [O:2] array. Can anyone help ?
data["people"][0:2] return list [{'craft': 'ISS', 'name': 'Alexey Ovchinin'}, {'craft': 'ISS', 'name': 'Nick Hague'}]
you should iterate list
name = [x['name'] for x in data["people"][0:2]]
print(name)
O/P:
['Alexey Ovchinin','Nick Hague']
Since data["people"][0:2] is a list (try type(data["people"][0:2] in ipython), you cannot use a string index to refer to its elements.
If what you want is the list of name for the people indexed from 0 to 2 (2 not included) in the list, what you want is:
print( [x["name"] for x in data["people"][0:2] )

Convertig an int item from list for mysql command[python , mysql ]

I am writing a script for an MySql-Server and i need an int-variable from a list of integers for an mysql command.
The list ID looks like that (2028,) (1029,) (4000,)
If i use for example ID[1] and put this into the command i get an error , because the Server needs a pure int variable and not an item from a list.
The output looks like that :
print(ID[1]) (2028,)
But i need the variable like this :
print(ID[1]) 2028
Is there any converting command for this?
Try print (ID[1][0]). List is of tuples which contains int.
You can convert your list of tuples into list of integers.
For example if you have
data = [(2028,), (1029,), (4000,)]
you can do
>>> print [entry[0] for entry in data]
[2028, 1029, 4000]

Categories

Resources