Dolamite Jenkins
asked on
I use the'%s" %row and I keep getting and error
my db is returning the query correctly but its printing like below even when I use the'%s" %row and I keep getting and error when I use '\n" because I want every returned query on its own line... How do I do that ...
con = lite.connect('sms.sqlite')
cur = con.cursor()
cur.execute("select First_Name, Last_Name, Age, Gender, Address, City, State from records")
rows=cur.fetchall()
print "%s" %rows\n
canvas = canvas.Canvas("logsheet.pdf", (1275,1650))
#canvas.drawImage("template_summary.png",0,0)
canvas.setLineWidth(.3)
canvas.setFont('Helvetica-Bold', 12)
canvas.drawString(640,1290, '%s' % rows\n)
canvas.drawString(700,1000,'BLOOD PRESSURE')
canvas.showPage()
canvas.save()
cur.close()
con.close()
(u'', u'', 18, u'Male', u'', u'', u'California'), (u'', u'', 18, u'Male', u'', u'', u''), (u'Amberly', u'Richmond', 18, u'Male', u'', u'', u''), (u'', u'', 18, u'Male', u'', u'', u' Arkansas'), (u'', u'', 18, u'', u'', u'', u''), (u'', u'', 18, u'', u'', u'', u''), (u'', u'', 18, u'', u'', u'', u''), (u'', u'', 18, u'', u'', u'', u''), (u'Richmond', u'Clay', 18, u'', u'gggdh', u'bbb', u'Alaska'), (u'', u'', 18, u'', u'', u'', u''), (u'', u'', 25, u'', u'', u'', u''), (u'Richmond', u'Clay', 28, u'Male', u'', u'', u'California'), (u'', u'clay', 18, u'', u'', u'', u'California'), (u'', u'bbb', 18, u'', u'', u'', u''), (u'', u'cvbn', 18, u'', u'', u'', u''), (u'clay', u'rich', 18, u'', u'', u'', u'Alaska'), (u'ssss', u'ss', 18, u'', u'', u'', u''), (u'ssss', u'ssss', 18, u'', u'', u'', u''), (u'', u'sss', 18, u'', u'', u'', u''), (u'cghh', u'ffff', 18, u'', u'', u'', u''), (u'sdfgh', u'dsfghjk', 18, u'', u'', u'', u''), (u'', u'dfdfd', 18, u'', u'', u'', u''), (u'werty', u'qwerty', 18, u'', u'rfgthjuki', u'wertyu', u'')
You can also use the command
but it is rather cryptic. You have to think more about it to discover what it does. I do not prefer one-liners in such cases.
print '\n'.join(repr(row) for row in rows)
but it is rather cryptic. You have to think more about it to discover what it does. I do not prefer one-liners in such cases.
print would print the linefeed automatically.
The rest of the problem is because %s wants to print one item, and you have a list of lists. Use one of pepr's solutions to solve that problem.
The rest of the problem is because %s wants to print one item, and you have a list of lists. Use one of pepr's solutions to solve that problem.
A side not related to Superdave's comment...
The print "%s" % rows is not a problem (when removing the \n). It works the same as if you wrote the print "%s" % repr(rows), which is exactly the same as print repr(rows), which is the same as print rows. In all cases it prints a single line. When more \n are to be inserted, a loop must be used. Or the loop must be explicit, or it must be hidden -- as in '\n'.join() or list comprehension,...
The print "%s" % rows is not a problem (when removing the \n). It works the same as if you wrote the print "%s" % repr(rows), which is exactly the same as print repr(rows), which is the same as print rows. In all cases it prints a single line. When more \n are to be inserted, a loop must be used. Or the loop must be explicit, or it must be hidden -- as in '\n'.join() or list comprehension,...
... kidding. Forget the list comprehension.
ASKER
pepr Thanks and I understand sorta now about splitting line but how do I get (u,) out ?
The u'' or u'some string' are the representations of the unicode string values (hence the u). You have got it because you left the print command without any other hints. The print command converts automatically the arguments to strings. Often, the string conversion is the same as the repr() conversion. If possible, the repr() converts the object to the form that could be copy/pasted to the source code to obtain the same object. This is the case of your records. The list of rows was split to records where each record is a tuple with elements of some type. The representation of a tuple is build out of the representations of the elements, and everything is wrapped in parentheses.
Try the following snippet:
b.py
It prints
This is probably not what you would like (because of poor formatting -- elements separated by one space). However, it demonstrates that the element objects (i.e. unicode strings and integers) support not only the "technical representation" conversion, but also the "nicely readable string" conversion. Because of this the string elements were displayed withou the u''.
Try the following snippet:
b.py
rows = [ (u'', u'', 18, u'Male', u'', u'', u'California'),
(u'', u'', 18, u'Male', u'', u'', u''),
(u'Amberly', u'Richmond', 18, u'Male', u'', u'', u''),
(u'', u'', 18, u'Male', u'', u'', u' Arkansas'),
]
for record in rows:
print record # prints representation of the record (the tuple)
print u'-' * 70
for record in rows: # the loop through all records
for element in record: # the loop through all elements of the record
print element, # notice the final comma -- suppress the newline
print # the new line explicitly
It prints
E:\disk_C\tmp\_Python\dolamitejenkins\Q_27245503>python b.py
(u'', u'', 18, u'Male', u'', u'', u'California')
(u'', u'', 18, u'Male', u'', u'', u'')
(u'Amberly', u'Richmond', 18, u'Male', u'', u'', u'')
(u'', u'', 18, u'Male', u'', u'', u' Arkansas')
----------------------------------------------------------------------
18 Male California
18 Male
Amberly Richmond 18 Male
18 Male Arkansas
This is probably not what you would like (because of poor formatting -- elements separated by one space). However, it demonstrates that the element objects (i.e. unicode strings and integers) support not only the "technical representation" conversion, but also the "nicely readable string" conversion. Because of this the string elements were displayed withou the u''.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks I now have a better understanding of formatting
Open in new window
byOpen in new window