How to print both strings in a dictionary in Python - python

I'm having trouble printing both the name in the list and the email. The beginning part of the code was already pre written and my attempt is the for loop. If anybody would be able to help me that would be greatly appreciated.
Here are the directions:
Write a for loop to print each contact in contact_emails. Sample output for the given program:
mike.filt#bmail.com is Mike Filt
s.reyn#email.com is Sue Reyn
narty042#nmail.com is Nate Arty
Code:
contact_emails = {
'Sue Reyn' : 's.reyn#email.com',
'Mike Filt': 'mike.filt#bmail.com',
'Nate Arty': 'narty042#nmail.com'
}
for email in contact_emails:
print('%s is %s' % (email, contact_emails(email)))

Your problem is that you need to use square brackets([]) instead of parenthesis(()). Like so:
for email in contact_emails:
print('%s is %s' % (contact_emails[email], email)) # notice the []'s
But I recommend using the .items()( that would be .iteritems() if your using Python 2.x) attribute of dicts instead:
for name, email in contact_emails.items(): # .iteritems() for Python 2.x
print('%s is %s' % (email, name))
Thanks to #PierceDarragh for mentioning that using .format() would be a better option for your string formatting. eg.
print('{} is {}'.format(email, name))
Or, as #ShadowRanger has also mentioned that taking advantage of prints variable number arguments, and formatting, would also be a good idea:
print(email, 'is', name)

A simple way to do this would be using for/in loops to loop through each key, and for each key print each key, and then each value.
Here's how I would do it:
contact_emails = {
'Sue Reyn' : 's.reyn#email.com',
'Mike Filt': 'mike.filt#bmail.com',
'Nate Arty': 'narty042#nmail.com'
}
for email in contact_emails:
print (contact_emails[email] + " is " + email)
Hope this helps.

for email in contact_emails:
print("{} is {}".format(contact_emails[email], email))

Related

Unable to extract required field from JSON text

I am trying to extract a specific field "Engineering Lead" and its corresponding value from the JSON text but however, when tried to extract it directly from the JSON, it is throwing the key error as shown in the code1 . Since it is not working, i have decided to loop it to fetch the key Engineering Lead and it is value but it still throwing the same error. any help would be aprreciated.
json text:
{'expand': 'renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations', 'id': '11659640', 'self': '/rest/api/2/issue/11659640', 'key': 'TOOLSTEST-2651', 'fields': {'description': 'h2. Main\r\n * *:*\r\n * *Application ISO:*\xa0Tony Zeinoun\r\n * *Engineering Lead:*\xa0Peter james\r\n * *Application Architect:*\xa0John david\r\n * *Divisional Architect:*\xa0Robert denuvit'}}
code 1:
engLeadDetails = data_load['fields']['* \*Engineering Lead']
Code 2:
engLeadDetails = data_load['fields']
for k,v in engLeadDetails.items():
if (k == '* \*Engineering Lead'):
print (v)
Error:
Traceback (most recent call last):
File "/Users/peter/abc.py", line 32, in <module>
engLeadDetails = data_load['fields']['* *Engineering Lead']
KeyError: '* *Engineering Lead'
I think python can't find such key because of some missing quotes. Please check the json text once more. It seems like * *Engineering Lead is currently a part of a bigger string, but not a key (due to missing quotes).
KeyError means that the key * \*Engineering Lead doesn't exist in the dictionary.
It appears the delimiter of the description (where your EngLead is stored) is \r\n.
Using this we can split the description to get each role.
job_details = data_load["fields"]["description"]
Removing arbitrary strings, this leaves us with
job_details = [
"* *Application ISO:* Tony Zeinoun",
"* *Engineering Lead:* Peter james",
"* *Application Architect:* John david",
"* *Divisional Architect:* Robert denuvit",
]
I am assuming you want the name of the person in each position.
Now we remove arbitrary characters from each string.
job_dict = {}
for s in job_details:
s = s.replace("*","").strip()
job, person = s.split(":")
job_dict[job] = person.strip()
job_dict is now clean, with easy key access to each job.
Resultant Dict:
{
'Application Architect': 'John david',
'Application ISO': 'Tony Zeinoun',
'Divisional Architect': 'Robert denuvit',
'Engineering Lead': 'Peter james'
}
print(job_dict["Engineering Lead"]) # Peter james
You can convert the JSON string description into a dictionary by splitting on \r\n sequence then break the role and name into key/values and add to a dictionary.
The expression \W* in regexp below will strip off the non-alphanumeric prefix off the roles and names; e.g., "** Application ISO" => "Application ISO", etc.
Try something like this:
data = {}
for s in data_load['fields']['description'].split('\r\n'):
if m := re.search(r'^\W*(.*?):\W*(.+)', s):
if label := m.group(1):
data[label] = m.group(2)
print(data)
Output:
{'Application ISO': 'Tony Zeinoun', 'Engineering Lead': 'Peter james', 'Application Architect': 'John david', 'Divisional Architect': 'Robert denuvit'}
Then can grab a particular role/person out:
print(">>", data.get("Engineering Lead"))
Outputs:
>> Peter james

How to print only 1 item from a dictionary

I've recently just started learning Python and usally find answers to my problems online but can't seem to get the right solution for this one.
I created a dictionary with 3 contacts and i want to print 1 contact from the list using an if statement.
contacts = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
if "John" in contacts:
print ("Contact details: %s %i" % contacts.items()[0])
This is the output I am looking for:
Contact details: John 938477566
But i keep getting this
Traceback (most recent call last):
File "C:\Users\user\Documents\asega\python\objectsclasses\exercise3.py", line 31, in
print ("Contact details: %s %i" % contacts.items()[0])
TypeError: 'dict_items' object does not support indexing
Thanks
contacts.items() returns pairs of key-value. In your case, that would be something like
(("John", 938477566), ("Jack", 938377264), ("Jill", 947662781))
Except that in python 3 this is like a generator and not a list. So you'd have to do list(contacts.items()) if you wanted to index it, which explains your error message. However, even if you did list(contacts.items())[0], as discussed above, you'd get the first pair of key-value.
What you're trying to do is fetch the value of a key if said key exists and contacts.get(key, value_if_key_doesnt_exist) does that for you.
contact = 'John'
# we use 0 for the default value because it's falsy,
# but you'd have to ensure that 0 wouldn't naturally occur in your values
# or any other falsy value, for that matter.
details = contacts.get(contact, 0)
if details:
print('Contact details: {} {}'.format(contact, details))
else:
print('Contact not found')
You can do like this. If you are sure that 'John' is in the dictionary, there is no need of if statement. In other way, you can write it.. your choice
contacts = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
print("Contact details: %s %i" % ("John", contacts["John"]))
First of all its dictionary not a list , you can access the elements from a list by indexing , and it is not possible in dictionary,
you can access the elements by key
contacts = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
for k,v in contacts.items():
print(k,v)
or
contacts['John'] you can access the value
No need to check in if condition just use get to get the corresponding value and return -1 if key is not present
contacts = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
contacts.get('John',-1) # -1 will be returned if key is not found
Print formatting
name_to_search='John'
print("Contact details: %s %i" % (name_to_search, contacts.get(name_to_search,-1)))
Or
name_to_search='John'
print("Contact details: {} {}" .format(name_to_search, contacts.get(name_to_search,-1)))
Dictionaries do not support indexing so to print "John" you cant index it however the following code may word:
if "John" in contacts:
print("Contact details:","John",contacts["John"])
Hope it helps

Split string, unicode, unicode, string in python

I was trying to split combination of string, unicode in python. The split has to be made on the ResultSet object retrieved from web-site. Using the code below, I am able to get the details, actually it is user details:
from bs4 import BeautifulSoup
import urllib2
import re
url = "http://www.mouthshut.com/vinay_beriwal"
profile_user = urllib2.urlopen(url)
profile_soup = BeautifulSoup(profile_user.read())
usr_dtls = profile_soup.find("div",id=re.compile("_divAboutMe")).find_all('p')
for dt in usr_dtls:
usr_dtls = " ".join(dt.text.split())
print(usr_dtls)
The output is as below:
i love yellow..
Name: Vinay Beriwal
Age: 39 years
Hometown: New Delhi, India
Country: India
Member since: Feb 11, 2016
What I need is to create distinct 5 variables as Name, Age, Hometown, Country, Member since and store the corresponding value after ':' for same.
Thanks
You can use a dictionary to store name-value pairs.For example -
my_dict = {"Name":"Vinay","Age":21}
In my_dict, Name and Age are the keys of the dictionary, you can access values like this -
print (my_dict["Name"]) #This will print Vinay
Also, it's nice and better to use complete words for variable names.
results = profile_soup.find("div",id=re.compile("_divAboutMe")).find_all('p')
user_data={} #dictionary initialization
for result in results:
result = " ".join(result.text.split())
try:
var,value = result.strip().split(':')
user_data[var.strip()]=value.strip()
except:
pass
#If you print the user_data now
print (user_data)
'''
This is what it'll print
{'Age': ' 39 years', 'Country': ' India', 'Hometown': 'New Delhi, India', 'Name': 'Vinay Beriwal', 'Member since': 'Feb 11, 2016'}
'''
You can use a dictionary to store your data:
my_dict = {}
for dt in usr_dtls:
item = " ".join(dt.text.split())
try:
if ':' in item:
k, v = item.split(':')
my_dict[k.strip()] = v.strip()
except:
pass
Note: You should not use usr_dtls inside your for loop, because that's would override your original usr_dtls

Python: split string with delimiters from a list

I'd like to split a string with delimiters which are in a list.
The string has this pattern: Firstname, Lastname Email
The list of delimiters has this: [', ',' '] taken out of the pattern.
I'd like to split the string to get a list like this
['Firstname', 'Lastname', 'Email']
For a better understanding of my problem, this is what I'm trying to achieve:
The user shall be able to provide a source pattern: %Fn%, %Ln% %Mail% of data to be imported
and a target pattern how the data shall be displayed:
%Ln%%Fn%; %Ln%, %Fn; %Mail%
This is my attempt:
data = "Firstname, Lastname Email"
for delimiter in source_pattern_delimiter:
prog = re.compile(delimiter)
data_tuple = prog.split(data)
How do I 'merge' the data_tuple list(s)?
import re
re.split(re.compile("|".join([", ", " "])), "Firstname, Lastname Email")
hope it helps
Seems you want something like this,
>> s = "Firstname, Lastname Email"
>>> delim = [', ',' ']
>>> re.split(r'(?:' + '|'.join(delim) + r')', s)
['Firstname', 'Lastname', 'Email']
A solution without regexes and if you want to apply a particular delimiter at a particular position:
def split(s, delimiters):
for d in delimiters:
item, s = s.split(d, 1)
yield item
else:
yield s
>>> list(split("Firstname, Lastname Email", [", ", " "]))
["Firstname", "Lastname", "Email"]
What about splitting on spaces, then removing any trailing commas?
>>> data = "Firstname, Lastname Email"
>>> [s.rstrip(',') for s in data.split(' ')]
['Firstname', 'Lastname', 'Email']
You are asking for a template based way to reconstruct the split data. The following script could give you an idea how to progress. It first splits the data into the three parts and assigns each to a dictionary entry. This can then be used to give a target pattern:
import re
data = "Firstname, Lastname Email"
# Find a list of entries and display them
entries = re.findall("(\w+)", data)
print entries
# Convert the entries into a dictionary
dEntries = {"Fn": entries[0], "Ln": entries[1], "Mail": entries[2]}
# Use dictionary-based string formatting to provide a template system
print "%(Ln)s%(Fn)s; %(Ln)s, %(Fn)s; %(Mail)s" % dEntries
This displays the following:
['Firstname', 'Lastname', 'Email']
LastnameFirstname; Lastname, Firstname; Email
If you really need to use the exact template system you have provided then the following could be done to first convert your target pattern into one suitable for use with Python's dictionary system:
def display_with_template(data, target_pattern):
entries = re.findall("(\w+)", data)
dEntries = {"Fn": entries[0], "Ln": entries[1], "Mail": entries[2]}
for item in ["Fn", "Ln", "Mail"]:
target_pattern= target_pattern.replace("%%%s%%" % item, "%%(%s)s" % item)
return target_pattern % dEntries
print display_with_template("Firstname, Lastname Email", r"%Ln%%Fn%; %Ln%, %Fn%; %Mail%")
Which would display the same result, but uses a custom target pattern:
LastnameFirstname; Lastname, Firstname; Email

Python: Problems when iterating over a dictionary

So, I have been working on this simple Python program to get familiar with dictionaries. Basically, it works as a database which you can search in. If your entry is in the dictionary key, it brings up the information regarding the entry.
Family = {'Jim' : ['cool guy', 'has facial hair'],
'Ned' : ['hot stuff', ' wears Tees']}
query = input("Look up database on whom? > ")
for (name, info) in Family.items():
if name in query or name.lower() in query:
print("{} is {}".format(name, info))
This ^ works. However, when I tried to add an ELSE clause, to deal with non-existent entries, I get this.
else:
print ('Value not found!')
It prints the Value not found! many times before bringing up the value. If I try to add a 'go back to start' function it doesn't even bring up a registered value. I know this is because it is a loop and iterates over the dict one by one; so like 1)jim is true then 2) ned is false.
How do I improve this code to make it: -able to give an error about a non-existent entry and then restart the program. Thanks.
You will need to take care of case insensitivity in your code. Iterate through the list to ensure that the name exists before continuing:
Family = {'Jim' : ['cool guy', 'has facial hair'],
'Ned' : ['hot stuff', ' wears Tees']}
names = [name.lower() for name in Family]
def find(query):
if query.lower() in names:
info = [Family[n] for n in Family if n.lower() == query.lower()]
print('{} is {}'.format(
query, info
))
else:
print('{} not found'.format(query))
If you try it with the following sample:
find('Ned')
find('ned')
find('no ned')
You will get the following results:
Ned is [['hot stuff', ' wears Tees']]
ned is [['hot stuff', ' wears Tees']]
no ned not found
This is one way to do it:
Family = {'Jim' : ['cool guy', 'has facial hair'],
'Ned' : ['hot stuff', ' wears Tees']}
query = input("Look up database on whom? > ")
if query in Family.keys():
for (name, info) in Family.items():
if name in query or name.lower() in query:
print("{} is {}".format(name, info))
else:
print "Print Something - Not in Family"

Categories