微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python将字符串与具有转义字符的字符串变量连接起来

如何解决Python将字符串与具有转义字符的字符串变量连接起来

我在 python 脚本中有一个命令,如下所示:

start_command = [
        "load=" + location + "/lib/abc " + "conf=myconf " +
        location + "/bin/MysqLd "
        "--defaults-file=" + location + "/var/myfile.cnf " 
        ]

    

我想将代码的第二部分作为变量(connect_str)推送并在 start 命令中连接。但它不起作用。这是因为转义字符吗?

 connect_str="conf=myconf " +
        location + "/bin/MysqLd "
        "--defaults-file=" + location + "/var/myfile.cnf "
 start_command = [
        "load=" + location + "/lib/abc " + connect_str

解决方法

如果你想连接 str 它应该是 str ,我在你的代码中看到一个列表,并且没有转义字符是这个 \ 在你有 / 。我建议你像这样使用格式要好得多

connect_str="conf=myconf {} /bin/mysqld --defaults-file={} /var/myfile.cnf ".format(location,location)
,

是的,使用 \ 转义 \n

connect_str = "conf=myconf " + location + \
    " /bin/mysqld " "--defaults-file=" + location + \
    " /var/myfile.cnf "
,

我的错误在于声明了我更改并使其工作的字符串变量。实际是多行注释问题。

connect_str="conf=myconf " + location + "/bin/mysqld " "--defaults-file=" + location + "/var/myfile.cnf "
 start_command = [
        "load=" + location + "/lib/abc " + connect_str
          ]

有没有比将问题放在一行更好的方法来解决问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。