简体   繁体   English

如何在Python中关闭多行变量/注释?

[英]How should I close a multi-line variable/comment in Python?

I am receiving this error: 我收到此错误:

  File "/DateDbLoop.py", line 33  
    d.Id""" % (str(day), str(2840))"  
    ^  
SyntaxError: EOL while scanning single-quoted string  

Here is the script. 这是脚本。 There are 4 double quotes to open this, but I am unsure how to correctly close this out? 有4个双引号将其打开,但是我不确定如何正确将其关闭?

Follow Up Question: 后续问题:

Does this % (str(day), str(2840)) need to go in both the sql variable and the os.system() call? 这个%(str(day),str(2840))是否需要同时进入sql变量和os.system()调用?

#!/usr/bin/python

import datetime
import sys, os, time, string

a = datetime.date(2009, 1, 1)
b = datetime.date(2009, 2, 1)
one_day = datetime.timedelta(1)

day = a

while day <= b:

    print "Running query for \"" + str(day) + "\""

    sql=""""SELECT
        d.Date,  
        SUM(d.Revenue),  
        FROM Table d  
        WHERE d.Date = '%s'  
        AND d.Id = %s  
        GROUP BY d.Date  
        """ % (str(day), str(2840))"

    os.system('mysql -h -sN -u  -p -e %s > FileName-%s.txt db' % (sql, str(day)))
    day += one_day

You have 4 double-quotes at your sql= line, make it 3 instead. 在sql =行中有4个双引号,改为3。 Also remove the single quote after your %-substitution value. 另外,请在您的%-substitution值之后删除单引号。

#!/usr/bin/python

import datetime
import sys, os, time, string

a = datetime.date(2009, 1, 1)
b = datetime.date(2009, 2, 1)
one_day = datetime.timedelta(1)

day = a

while day <= b:
    print "Running query for \"" + str(day) + "\""

    sql="""SELECT
    d.Date,  
    SUM(d.Revenue)
    FROM Table d  
    WHERE d.Date = '%s'  
    AND d.Id = %s  
    GROUP BY d.Date  
    """ % (str(day), str(2840))

    os.system('mysql -h -sN -u  -p -e "%s" > FileName-%s.txt db' % (sql, str(day)))
    day += one_day

Multi-line string values are done with paired triplets of double-quotes in Python, and don't nest inside regular double-quotes. 多行字符串值是在Python中使用成对的三重双引号完成的,并且不嵌套在常规双引号内。

Open and close the string with three quotes 用三个引号打开和关闭字符串

sql = """
      SELECT d.Date, SUM(d.Revenue),
      FROM Table d WHERE d.Date = '%s' AND d.Id = %s 
      GROUP BY d.Date
      """ % (str(day), str(2840))

You can also break a line in the middle of a string with the \\ character. 您也可以使用\\字符在字符串中间断开一行。

#!/usr/bin/python

import datetime
import sys, os, time, string

a = datetime.date(2009, 1, 1)
b = datetime.date(2009, 2, 1)
one_day = datetime.timedelta(1)

day = a

while day <= b:

 print "Running query for \"" + str(day) + "\""

 sql="SELECT d.Date, SUM(d.Revenue), FROM Table d WHERE d.Date = '%s' \
      AND d.Id = %s GROUP BY d.Date " % (str(day), str(2840))

 os.system('mysql -h -sN -u  -p -e %s > FileName-%s.txt db' % (sql, str(day)))

You use triple quotes for this. 为此使用三引号。

s = """
Python is awesome.
Python is cool.
I use Python.
And so should you.
"""

print s

Python is awesome.
Python is cool.
I use Python.
And so should you.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM