Psycopg2 | collapse row to string

Demel, Jeff Jeff.Demel at JavelinDirect.com
Tue Nov 21 16:44:18 EST 2006


resultfile.write(delimiter.join([str(elt) for elt in row]))

Worked like a charm.  Thanks, Skip.

-Jeff



-----Original Message-----
From: skip at pobox.com [mailto:skip at pobox.com] 
Sent: Tuesday, November 21, 2006 3:19 PM
To: Demel, Jeff
Cc: python-list at python.org
Subject: Re: Psycopg2 | collapse row to string


    Jeff> Is there an easy want to create a delimited string from this,
for
    Jeff> example:

    Jeff> for row in rows:
    Jeff>     if checksomething == 100:
    Jeff>         newline = row[].combine("delimiter of some kind")
    Jeff>         resultfile.write(newline)

By "delimited string" do you mean something that can handle fields in
which your delimiter appears?  If so, you should look at the csv.writer
class.  If you're absolutely certain your delimiter won't occur in your
output, you can just stringify the values and join them:

    delimiter = ","
    for row in rows:
        if checksomething == 100:
            resultfile.write(delimiter.join([str(elt) for elt in row]))

If you know all your row values are already strings it's even easier:

    delimiter = ","
    for row in rows:
        if checksomething == 100:
            resultfile.write(delimiter.join(row))

Skip
This email is intended only for the individual or entity to which it is addressed.  This email may contain information that is privileged, confidential or otherwise protected from disclosure. Dissemination, distribution or copying of this e-mail or any attachments by anyone other than the intended recipient, or an employee or agent responsible for delivering the message to the intended recipient, is prohibited. If you are not the intended recipient of this message or the employee or agent responsible for delivery of this email to the intended recipient, please notify the sender by replying to this message and then delete it from your system.  Any use, dissemination, distribution, or reproduction of this message by unintended recipients is strictly prohibited and may be unlawful.



More information about the Python-list mailing list