Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47341

Trying to work with data from a query using Python.

Newsgroups comp.lang.python
Date 2013-06-07 10:44 -0700
Message-ID <3f8d60b3-6b92-4670-ac99-28ec6fee580b@googlegroups.com> (permalink)
Subject Trying to work with data from a query using Python.
From ethereal_robe@hotmail.com

Show all headers | View raw


Hello, I'm working with PostgreSQL and Python to obtain 2 columns froma  database and need to print it in a specific format.

Here is my current code.



#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys

con = None

try:
     
    con = psycopg2.connect(database='DB', user='ME', password='1234')  
    
    cur = con.cursor()    
    cur.execute(" select Account_Invoice.amount_untaxed, right (Res_Partner.vat,length(Res_Partner.vat)-2) as RFC from Account_Invoice inner join Res_Partner on Account_Invoice.partner_id = Res_Partner.id inner join Account_Invoice_Tax on Account_Invoice.id = Account_Invoice_Tax.invoice_id where account_invoice.journal_id=2 and account_invoice.date_invoice >= '2013-01-01' and account_invoice.date_invoice <= '2013-02-01' and account_invoice.reconciled is TRUE and account_invoice_tax.account_id = 3237 and account_invoice.amount_tax >= 0;")

    rows = cur.fetchall()

    for row in rows:
        print row
    

except psycopg2.DatabaseError, e:
    print 'Error %s' % e    
    sys.exit(1)
    
    
finally:
    
    if con:
        con.close()




Now assume that fetchall would print the following:

LOEL910624ND5 from the column vat as RFC.
227 from the column amount_untaxed.


Now I would need to print that in the following format.

04|85|LOEL910624ND5|||||227|||||||||||||||

04 always goes in the first column and 85 always goes in the second, vat goes in the third and the amount_untaxed goes in the eight column but we still need to have 22 columns in total.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Trying to work with data from a query using Python. ethereal_robe@hotmail.com - 2013-06-07 10:44 -0700
  Re: Trying to work with data from a query using Python. Dave Angel <davea@davea.name> - 2013-06-07 14:24 -0400
    Re: Trying to work with data from a query using Python. Walter Hurry <walterhurry@lavabit.com> - 2013-06-07 21:59 +0000
  Re: Trying to work with data from a query using Python. Peter Otten <__peter__@web.de> - 2013-06-07 20:59 +0200

csiph-web