在没有附加sp的情况下将此字符串分解为代码

2024-10-01 19:18:45 发布

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

我有一根长长的绳子,像:

    cmd = "python %sbin/datax.py -p '-Drun=%s -Drpwd=%s -Drjdbc=%s -Dcond=%s -Dtable=%s \ 
                                 -Dwun=%s -Dwpwd=%s -Dwjdbc=%s' %s" % ( DATAX_HOME,
                                                                      rds_reader['username'],
                                                                      rds_reader['password'],
                                                                      sub_jdbc_url_generator(args.reader),
                                                                      where_condition,
                                                                      args.table,
                                                                      rds_writer['username'],
                                                                      rds_writer['password'],
                                                                      sub_jdbc_url_generator(args.writer),
                                                                      job_template_file)

我不想把所有的-D放在一行中,因为这看起来太长,而且上面的代码实际上可以工作,但它返回:

python /tmp/datax/bin/datax.py -p '-Drun=xxx ... -Dtable=demo                                      -Dwun=yyy ...'

结果里面有很长的空间。我也读了一些问题,但这个字符串包含一些%s要填充的内容。你知道吗

那么如何解决这个问题呢?还是另一种优雅的写作方式?感谢您的帮助。你知道吗


预期输出:

python /tmp/datax/bin/datax.py -p '-Drun=xxx ... -Dtable=demo -Dwun=yyy ...'

Tags: pyurlusernameargspasswordgeneratortmpreader
1条回答
网友
1楼 · 发布于 2024-10-01 19:18:45

Python将连接两个相邻的字符串。带引号的字符串之间的间距将被丢弃。例如:

print("something "    "something")

输出:

something something

因此,您可以通过使用两个完整的字符串来扩展行,其中一个是行继续(\),另一个是将字符串括在圆括号中:

cmd1 = "python blah blah "\
       "more {} {} blah".format('abc',123)

cmd2 = ("python blah blah "
        "{} {} "
        "more stuff").format('abc',123)

print(cmd1)
print(cmd2)

输出:

python blah blah more abc 123 blah
python blah blah abc 123 more stuff

相关问题 更多 >

    热门问题