使用INI文件与密码使用特殊字符使用Python configparser

发布于 2025-01-21 10:42:29 字数 412 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昇り龍 2025-01-28 10:42:29

密码仍然相同,但其表示形式使您感到困惑。
问题是“ \ 7”,可以解释为特殊角色。为了避免任何混乱,Python写了双反斜击。
要查看差异,您可以编写以下两个字符串:

string_1 = "\7"
string_2 = r"\7"
print(string_1)
print(string_2)

print(repr(string_1))
print(repr(string_2))

我希望这可以帮助您了解Python显示的内容的区别。

编辑:

不应该有额外的反弹。请尝试以下操作:

import configparser

content = r"""
[main]
user_name = username
password = [k!:SU`T&m5@3D\\7Z
"""
with open('test.ini', mode='w') as file:
    file.write(content)
parser = configparser.ConfigParser()
with open('test.ini', mode='r') as file:
    parser.read_file(file)
assert parser['main']['password'] == r"[k!:SU`T&m5@3D\\7Z"

edit2:请尝试编辑的代码。它应该创建您的INI文件,并用主张测试的正确密码阅读。

edit3:
我只是复制粘贴您的INI文件,并运行您的行并获得正确的输出而没有额外的砍伐:

username
[k!:SU`T&m5@3D\\7Z

因此,您可以显示以下行的输出:

import platform
import sys

print(sys.version)
print(platform.platform())
print(sys.getdefaultencoding())

在我的情况下,这是:

3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0]
Linux-4.12.14-lp151.28.91-default-x86_64-with-glibc2.10
utf-8

对不起,我无法进一步帮助您,因为我无法复制您的错误。

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:

string_1 = "\7"
string_2 = r"\7"
print(string_1)
print(string_2)

print(repr(string_1))
print(repr(string_2))

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:

import configparser

content = r"""
[main]
user_name = username
password = [k!:SU`T&m5@3D\\7Z
"""
with open('test.ini', mode='w') as file:
    file.write(content)
parser = configparser.ConfigParser()
with open('test.ini', mode='r') as file:
    parser.read_file(file)
assert parser['main']['password'] == r"[k!:SU`T&m5@3D\\7Z"

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:

username
[k!:SU`T&m5@3D\\7Z

So could you please show the output of the following lines:

import platform
import sys

print(sys.version)
print(platform.platform())
print(sys.getdefaultencoding())

which in my case says:

3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0]
Linux-4.12.14-lp151.28.91-default-x86_64-with-glibc2.10
utf-8

Sorry, I cannot help you further, because I cannot reproduce your error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文