tags:

views:

230

answers:

3

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?

Follow Up Question:

Does this % (str(day), str(2840)) need to go in both the sql variable and the os.system() call?

#!/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
A: 

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.
DevDevDev
@Steve: in case you did not know, you can highlight your code and press Ctrl-k to get it to be formatted in a monospace font with syntax highlighting.
Adam Bernier
Awesome! I thought you had to hit Ctrl+k for each line. This is much easier :)
DevDevDev
+4  A: 

You have 4 double-quotes at your sql= line, make it 3 instead. Also remove the single quote after your %-substitution value.

#!/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.

Amber
Triple single quotes would also work. Python doesn't distinguish between single or double, except you open and close strings with the same character(s).
Heikki Toivonen
Yep. Very handy when the thing you're wanting to quote has one type or another of quote mark already in it.
Amber
Thanks Dav. I made this change but now I am getting an new error. This time it is a mysql error:ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1I just realized: Do I need to have this % (str(day), str(2840)) in both the sql variable and the os.system() ?
David Perron
Ah, no, the issue is that since you're echoing it to the command line, it needs to be quoted on the command line. I'd use double quotes within your os.system call to do that - I'll edit the example code above to reflect it, note the "%s" instead of just %s now.
Amber
Also you needed to remove the comma after the SUM() in the select, since it's the last item before the FROM. I edited the example for that too.
Amber
That did it! Thanks so much Dav. I gotta say, python is picky, but I think I am starting to understand this...
David Perron
+1  A: 

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)))
Brian Gianforcaro