I'm using a package that downloads favicons from websites.
It has 4 components (Not key-value pairs) and its corresponding values for each element in the list.
url='https://www.python.org/'
width=200
height=200
format='png'
My purpose is to convert each element in the list(containing the 4 components) same into a key:value pair as in a dictionary in this way:
'url':'https://www.python.org/'
'width':200
'height':200
'format':'png'
My following attempt didnt yield the correct result.
Here is my attempt :
import favicon
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
headers = {'User-Agent': user_agent}
url = 'https://www.python.org/'
icons = favicon.get(url, headers=headers, timeout=2)
print(f"Before:{icons}")
my_dict = {}
for index,element in enumerate(icons):
my_dict[index] = element
print(f"After:{my_dict}")
The result of
print(f"Before:{icons}) is
[Icon(url='https://www.python.org/static/opengraph-icon-200x200.png', width=200, height=200, format='png'), Icon(url='https://www.python.org/static/metro-icon-144x144-precomposed.png', width=144, height=144, format='png'), Icon(url='https://www.python.org/static/apple-touch-icon-144x144-precomposed.png', width=144, height=144, format='png'), Icon(url='https://www.python.org/static/apple-touch-icon-114x114-precomposed.png', width=114, height=114, format='png'), Icon(url='https://www.python.org/static/apple-touch-icon-72x72-precomposed.png', width=72, height=72, format='png'), Icon(url='https://www.python.org/favicon.ico', width=0, height=0, format='ico'), Icon(url='https://www.python.org/static/apple-touch-icon-precomposed.png', width=0, height=0, format='png'), Icon(url='https://www.python.org/static/favicon.ico', width=0, height=0, format='ico')]
And the result after enumerating through it and building a dictionary:
print(f"After:{my_dict}") is
After:{0: Icon(url='https://www.python.org/static/opengraph-icon-200x200.png', width=200, height=200, format='png'), 1: Icon(url='https://www.python.org/static/metro-icon-144x144-precomposed.png', width=144, height=144, format='png'), 2: Icon(url='https://www.python.org/static/apple-touch-icon-144x144-precomposed.png', width=144, height=144, format='png'), 3: Icon(url='https://www.python.org/static/apple-touch-icon-114x114-precomposed.png', width=114, height=114, format='png'), 4: Icon(url='https://www.python.org/static/apple-touch-icon-72x72-precomposed.png', width=72, height=72, format='png'), 5: Icon(url='https://www.python.org/static/favicon.ico', width=0, height=0, format='ico'), 6: Icon(url='https://www.python.org/static/apple-touch-icon-precomposed.png', width=0, height=0, format='png'), 7: Icon(url='https://www.python.org/favicon.ico', width=0, height=0, format='ico')}
How it should look :
{Icon('url':'https://www.python.org/static/opengraph-icon-200x200.png', 'width':200, 'height':200, 'format':'png')}
My objective is to convert the equality into a key-value pair.
Kindly assist !
Given:
import collections
Icon = collections.namedtuple('Icon', ['url', 'width', 'height', 'format'])
icons = [
Icon(url='https://www.python.org/static/opengraph-icon-200x200.png', width=200, height=200, format='png'),
Icon(url='https://www.python.org/static/metro-icon-144x144-precomposed.png', width=144, height=144, format='png'),
Icon(url='https://www.python.org/static/apple-touch-icon-144x144-precomposed.png', width=144, height=144, format='png'),
Icon(url='https://www.python.org/static/apple-touch-icon-114x114-precomposed.png', width=114, height=114, format='png'),
Icon(url='https://www.python.org/static/apple-touch-icon-72x72-precomposed.png', width=72, height=72, format='png'),
Icon(url='https://www.python.org/favicon.ico', width=0, height=0, format='ico'),
Icon(url='https://www.python.org/static/apple-touch-icon-precomposed.png', width=0, height=0, format='png'),
Icon(url='https://www.python.org/static/favicon.ico', width=0, height=0, format='ico')
]
If you want to turn the list of collections.namedtuple()
above into a dictionary with keys of indexes then you can do this:
icons_dicts = {i:v._asdict() for i,v in enumerate(icons)}
print(icons_dicts)
If you want to turn that list of collections.namedtuple()
into a list of dictionaries you can do this.
icons_list = [v._asdict() for v in icons]
print(icons_list)
If neither of these outputs is what you seek please give an exact valid result example.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments