0

I have a long string like:

    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)

I do not want to put all -Ds in one row because that looks too long and the codes above works actually, but it returns:

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

It has a long space inside the result. I also read some questions but this string contains some %s to be filled up.

So how to tackle this issue? Or is it other elegant way to write? Any help is appreciated.


The expected output:

python /tmp/datax/bin/datax.py -p '-Drun=xxx ... -Dtable=demo -Dwun=yyy ...'
user2894829
  • 775
  • 1
  • 6
  • 26
  • @user5173426 Hi man. I just edit my question and put that at the end. – user2894829 Mar 04 '19 at 06:21
  • Possible duplicate of [How to declare a long string in Python?](https://stackoverflow.com/questions/8577027/how-to-declare-a-long-string-in-python) – Ignatius Mar 04 '19 at 06:23

1 Answers1

1

Python will concatenate two adjacent strings. the spacing between the quoted strings is discarded. For example:

print("something "    "something")

Outputs:

something something

So you can simply do the following by extending the line with two complete strings with either line continuation (\) or wrapping the strings in parentheses:

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

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

print(cmd1)
print(cmd2)

Output:

python blah blah more abc 123 blah
python blah blah abc 123 more stuff
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251