2015-04-20 52 views
1

http://legacy.python.org/dev/peps/pep-0008/#maximum-line-length帶分行符的內嵌評論

表示我們不需要超長的python字符串以方便閱讀。我的問題是,如果您想要斷行並且還包含內聯評論,則沒有指南。我有一個很長的查詢,乍看起來很模糊。我試着用一些行內把它清除掉評論

query = "select jobname,schedtab,odate,order_time,nodeid,state " \ 
     "from a{0}002_ajob," \ # midrange ajf jobs 
     "a{0}003_ajob," \ # mvs ajf jobs 
     "a{0}004_ajob," \ # ipo ajf jobs 
     "a{0}dv7_ajob" \ # aami ajf jobs 
     " where order_time < '{0}' order by odate;".format(date) 

我也曾嘗試

query = "select jobname,schedtab,odate,order_time,nodeid,state " \ 
     # midrange ajf jobs 
     "from a{0}002_ajob," \ 
     # mvs ajf jobs 
     "a{0}003_ajob," \ 
     # ipo ajf jobs 
     "a{0}004_ajob," \ 
     # aami ajf jobs 
     "a{0}dv7_ajob" \ 
     " where order_time < '{0}' order by odate;".format(date) 

兩個給我編譯器的問題。有任何想法嗎?

+0

見https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation和[這個問題](http://stackoverflow.com/q/17630835/242583)。 – livibetter

回答

1

只需加括號:

query = ("select jobname,schedtab,odate,order_time,nodeid,state " 
    "from a{0}002_ajob," # midrange ajf jobs 
    "a{0}003_ajob," # mvs ajf jobs 
    "a{0}004_ajob," # ipo ajf jobs 
    "a{0}dv7_ajob" # aami ajf jobs 
    " where order_time < '{0}' order by odate;").format(date) 

這工作太:

query = ("select jobname,schedtab,odate,order_time,nodeid,state " 
    # midrange ajf jobs 
    "from a{0}002_ajob," 
    # mvs ajf jobs 
    "a{0}003_ajob," 
    # ipo ajf jobs 
    "a{0}004_ajob," 
    # aami ajf jobs 
    "a{0}dv7_ajob" 
    " where order_time < '{0}' order by odate;").format(date)