使用INI文件与密码使用特殊字符使用Python configparser
new.ini文件
[main]
user_name = username
password = [k!:SU`T&m5@3D\\7Z
python代码
from configparser import ConfigParser
config = ConfigParser()
config.read(CONFIG.ini file path)
print(config["main"]["user_name"])
print(config["main"]["password"])
输出:
username
[k!:SU`T&m5@3D\\\\7Z
阅读文件后,解析的密码与文件输入密码不同。
new.ini file
[main]
user_name = username
password = [k!:SU`T&m5@3D\\7Z
python code
from configparser import ConfigParser
config = ConfigParser()
config.read(CONFIG.ini file path)
print(config["main"]["user_name"])
print(config["main"]["password"])
output:
username
[k!:SU`T&m5@3D\\\\7Z
After reading the file, the parsed password is not same as file input password.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
密码仍然相同,但其表示形式使您感到困惑。
问题是“ \ 7”,可以解释为特殊角色。为了避免任何混乱,Python写了双反斜击。
要查看差异,您可以编写以下两个字符串:
我希望这可以帮助您了解Python显示的内容的区别。
编辑:
不应该有额外的反弹。请尝试以下操作:
edit2:请尝试编辑的代码。它应该创建您的INI文件,并用主张测试的正确密码阅读。
edit3:
我只是复制粘贴您的INI文件,并运行您的行并获得正确的输出而没有额外的砍伐:
因此,您可以显示以下行的输出:
在我的情况下,这是:
对不起,我无法进一步帮助您,因为我无法复制您的错误。
The password is still the same, but its representation is confusing you.
The problem is the "\7" which could be interpreted as a special character. To avoid any confusion, python writes a double backslash.
To see the difference, you may write the following two strings:
I hope this helps you to understand the difference in what python displays.
Edit:
There should not be an extra backlash. Please try the following:
Edit2: Please try the edited code. It should create your ini file and read in the correct password as tested with the assertion.
Edit3:
I just copy pasted your ini file and ran your lines and get the correct output without extra slashes:
So could you please show the output of the following lines:
which in my case says:
Sorry, I cannot help you further, because I cannot reproduce your error.