Python Forum
[split] capitalize dict keys for display in string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] capitalize dict keys for display in string
#1
(Oct-10-2019, 07:09 AM)perfringo Wrote: In order to save repeated typing one can use dictionaries and try...except:

email = 'somebody@somewhere'                                                           
subject = 'I lost my license'                                                          
header = (f'Dear customer {email},\n\n' 
           'Thank you for contacting our company.') 
footer = 'Thank you\n\nCompany customer service'
categories = {'i lost my license': 'You will be contacted via email to verify you license request.\n\n',
              'problem saving file': 'Saving file is done by pressing CTRL-S (Windows} or CMD- (Mac).\n\n'}

try: 
    print(f'{header}{categories[subject.lower()]}{footer}') 
except KeyError: 
    print('Sorry, dada-dada-blah-blah') 
This way one needs to add only key-value pair to categories dictionary for additional subject.

Hi!

I'm not sure if it is appropriate for me to ask a question here, as it is not my thread. If it is not appropriate, my apologies, and I'd be grateful, relieved and agreeable if it is moved to a new thread.

Well, my question is that you, as usual, made wise suggestions to my always improvable code. (I'm very grateful to you for that, as it makes me widen my knowledge.) I made the necessary adjustments to my code and got this:

# license_02.py
#
 
email1 = input('Please, enter your email:\n')
subject1 = input('Please, enter your Message Subject:\n')
subject1 = subject1.lower()
messageBody1 = input('Please, enter your Message Body:\n')
                                                         
header = (f'Dear customer {email1},\n\n' 
           'Thank you for contacting our company.') 
footer = 'Thank you\n\nCompany customer service'
categories = {'i lost my license': 'You will be contacted via email to verify your license request.\n\n',
              'problem saving file': 'Saving file is done by pressing CTRL-S (Windows} or CMD- (Mac).\n\n'}

def getList(categories):
    for cat in categories.keys():
        return cat.capitalize()
 
try: 
    print(f'{header}{categories[subject1.lower()]}{footer}') 
except KeyError: 
    print(f"Sorry, you should write, word for word, one of these: '{getList(categories)}' on the message subject.")
    
and it works great on the two categories shown, but when I tried to improve also my code at the except KeyError:, by wanting to display all the keys of the dictionary, like this ideal output:
Output:
Sorry, you should write, word for word, one of these: 'I lost my license', 'Problem saving file', [ HERE FUTURE KEYS], on the message subject.
I managed to capitalize back the keys of the dictionary, but the only key printed is the first one:
Output:
Please, enter your email: User25@yeehaamail Please, enter your Message Subject: This is a test. Please, enter your Message Body: Blah blah blah ... Sorry, you should write, word for word, one of these: 'I lost my license' on the message subject. >>>
As usual, I tried different approaches, getting all kinds of error:
Error:
NameError: name 'capitalize' is not defined
Error:
AttributeError: 'list' object has no attribute 'keys'
Error:
TypeError: unhashable type: 'dict'
Error:
TypeError: unhashable type: 'dict_keys'
Error:
TypeError: unhashable type: 'slice'
Error:
TypeError: keys() takes no arguments (1 given)
Once again, I apologise if I'm posting in the wrong place. Please be free to move it where you see it feasible.

Thanks and all the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Messages In This Thread
[split] capitalize dict keys for display in string - by newbieAuggie2019 - Oct-10-2019, 10:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,521 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,135 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  How to display <IPython.core.display.HTML object>? pythopen 3 45,966 May-06-2023, 08:14 AM
Last Post: pramod08728
  [split] Parse Nested JSON String in Python mmm07 4 1,529 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Split string using variable found in a list japo85 2 1,300 Jul-11-2022, 08:52 AM
Last Post: japo85
  Updating nested dict list keys tbaror 2 1,283 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Loop Dict with inconsistent Keys Personne 1 1,610 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Split string knob 2 1,876 Nov-19-2021, 10:27 AM
Last Post: ghoul
  Create Dict from multiple Lists with duplicate Keys rhat398 10 4,080 Jun-26-2021, 11:12 AM
Last Post: Larz60+
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,236 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020