Python如何在字典中打印字符串中的反斜杠?

2024-06-23 20:11:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本字典有一些字符串,其中一个字符串中有两个反斜杠。我想用一个反斜杠代替它们。在

这些是反斜杠:IfNotExist\\u003dtrue

Configurations = {
        "javax.jdo.option.ConnectionUserName": "test",
        "javax.jdo.option.ConnectionDriverName": "org.mariadb.jdbc.Driver",
        "javax.jdo.option.ConnectionPassword": "sxxxsasdsasad",
        "javax.jdo.option.ConnectionURL": "jdbc:mysql://hive-metastore.cr.eu-west-1.rds.amazonaws.com:3306/hive?createDatabaseIfNotExist\\u003dtrue"

}
print (Configurations)

当我打印时,它总是显示两个反斜杠。我知道转义反斜杠的方法是使用\ this在常规字符串中有效,但在字典中不起作用。在

有什么想法吗?在


Tags: 字符串orgtest字典optionhive斜杠configurations
3条回答

打印字典将显示dictionary对象的表示形式。它不一定向你展示它里面所有的东西。要做到这一点:

for value in Configurations.values():
    print(value)

当你用 print (Configurations),它将打印出字典的repr()

你会得到

{'javax.jdo.option.ConnectionDriverName': 'org.mariadb.jdbc.Driver', 'javax.jdo.option.ConnectionUserName': 'test', 'javax.jdo.option.ConnectionPassword': 'sxxxsasdsasad', 'javax.jdo.option.ConnectionURL': 'jdbc:mysql://hive-metastore.cr.eu-west-1.rds.amazonaws.com:3306/hive?createDatabaseIfNotExist\\u003dtrue'}

你需要用 print (Configurations["javax.jdo.option.ConnectionURL"])

或者 print (str(Configurations["javax.jdo.option.ConnectionURL"]))注:str()增加

那么输出将是

jdbc:mysql://hive-metastore.cr.eu-west-1.rds.amazonaws.com:3306/hive?createDatabaseIfNotExist\u003dtrue

有关详细信息,请查看Python Documentation - Fancier Output Formatting

The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is no equivalent syntax).

问题来自编码。在

实际上,\u003d是{}的UNICODE表示。在

反斜杠被另一个反斜杠转义了,这是件好事。在

您可能需要:

  1. \u003d替换为=

  2. 将其读作unicode,然后应该在字符串前面加上u,比如u"hi \\u003d"可能没问题

相关问题 更多 >

    热门问题