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:
- [Solved] os.py“, line 725, in __getitem__ raise KeyError(key) from None KeyError: ‘PATH‘
- [Solved] Keras Error: KeyError: ‘accuracy‘, KeyError: ‘val_acc‘, KeyError: ‘acc‘
- [Solved] Loadrunner Error: with parameter delimiters is not a parameter.
- TypeError: unsupported format string passed to numpy.ndarray.__format__
- result = e.symbols[symb] KeyError: b‘system‘ [How to Solve]
- How to Fix KeyError: ‘plotly_domain’
- Maskrcnn-benchmark Error: KeyError “Non-existent config key: MODEL.BACKBONE.OUT_CHANNELS“
- [Solved] PYNQ load bit error: KeyError: ‘interrupts‘
- mmdetection Error when running voc.py: KeyError: ‘NumClassCheckHook is already registered in hook‘
- [Solved] rqt_graph Skipped loading plugin with error & Format: “dot“ not recognized
- [Solved] MindSpore Error: Data type conversion of ‘Parameter’ is not supporte
- ROS Error: warning: “deprecated pixel format used“
- [Solved] Android Compile and Package Error: Invalid keystore format
- insmod Error: Invalid module format [How to Solve]
- [Solved] HALCON error #5190: Invalid window parameter
- [Solved] leetcode Common Error: :runtime error: member access within misaligned address 0xbebebebebebebebe for type ‘str
- [Solved] Error d8021: invalid numeric parameter “/wno CPP” cython_bbox
- Oracle prompt text does not match format string
- [Solved] ERROR: lib/bridge_generated.dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
- How to Solve setsupportactionbar parameter Error