2015-04-20 25 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)