问题描述
我在 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
]
有没有比将问题放在一行更好的方法来解决问题?