如何修复 Python ValueError: unsupported format character ' ' (0x20) at index 3
问题:
你正在尝试使用类似这样的 Python 格式字符串
format_string_example.py
"Hello %.3 world" % 1.234但你看到类似这样的错误消息
valueerror_traceback.txt
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-7cbe94e4525d> in <module>
----> 1 "Hello %.3 world" % 1.234
ValueError: unsupported format character ' ' (0x20) at index 9解决方案
你的格式字符串 %.3 不完整!你需要指定要格式化的值类型,例如 f 用于浮点数,d 用于整数,s 用于字符串等。所以不要写 %.3,而是写 %.3f,或者不要只写 %,而是根据你要插入的变量的数据类型写例如 %f、%d 或 %s。
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow