Python3 str.format Keyerror solution for incoming parameter error
Additional knowledge of keyerror description and solution
Keyerror description and solution
If the parameter is called in the way of ‘W’, keyerror will be generated
# Define the variable c
>>>c = {'w':'w', 'o': 'o', 'r': 'r', 'l': 'l', 'd': 'd'}
{'w':'w', 'o': 'o', 'r': 'r', 'l': 'l', 'd': 'd'}
# Calling the parameter with 'w' will generate a KeyError error
>>>"Hello, {'w'}{'o'}{'r'}{'l'}{'d'}!".format(**c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'w'
Solution:
# Remove the single quotes and it'll be fine
>>> "Hello, {o}{r}{l}{d}!".format(**c)
Hello, world!
But I don’t understand the principle, I need to continue to learn.
Supplementary knowledge
When dict() is called to generate a dictionary, the key reference does not need to add single quotation marks.
>>>dict(w='w', o='o', r='r')
{'w': 'w', 'o': 'o', 'r': 'r'}
Read More:
- django Internal server error 500 Modify source code to view print errors
- Error in Tensorflow using variables: List of Tensors when single Tensor expected
- Java handles special characters in URL
- PIP Fatal error in launcher: Unable to create process using
- No match for ‘operator =’ both ends of the equal sign do not match
- Android Error: Found item attr/tabtextsize more than one time
- On error resume next, on error goto 0, err usage
- How to Solve ModuleNotFoundError: No module named ‘_bz2‘
- How to Fix KeyError: ‘plotly_domain’
- TypeError: unsupported format string passed to numpy.ndarray.__format__
- Oracle prompt text does not match format string
- Template Render Error Solution (Hexo blog theme, GIT upload)
- TypeException: Error setting non null for parameter #1 with JdbcType null
- The solution of no module named ‘jinja2. Asyncsupport’
- GO Exception Runnerw.exe: CreateProcess failed with error 21
- Plink Error: Multiple instances of ‘_’ in sample ID.?
- Numpy.exp Function Error ‘Float’ object has no attribute ‘exp’
- ValueError: Input 0 of node import/save/Assign was passed float from import/beta1_power:0 incompatib
- Creating test database for alias ‘postgres’… Got an error creating the test database: permission
- 500 (International server error) server error in Flash + heroku