Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47348
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '-*-': 0.07; 'column': 0.07; 'finally:': 0.07; 'postgresql': 0.07; 'subject:query': 0.07; 'sys': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rfc': 0.09; 'rows': 0.09; 'subject:using': 0.09; 'try:': 0.09; 'python': 0.11; 'assume': 0.14; 'template': 0.14; "%s'": 0.16; '-1,': 0.16; 'columns': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:0 40': 0.16; 'sys.exit(1)': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'select': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '"you': 0.24; 'skip:" 30': 0.26; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'skip:( 40': 0.30; "i'm": 0.30; 'assert': 0.31; 'second,': 0.31; 'skip:# 10': 0.33; 'third': 0.33; 'copying': 0.34; 'subject:from': 0.34; 'subject:with': 0.35; 'except': 0.35; 'but': 0.35; 'format.': 0.36; 'subject:data': 0.36; 'vat': 0.36; 'yield': 0.36; 'to:addr:python-list': 0.38; 'obtain': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'skip:a 30': 0.61; 'first': 0.61; 'more': 0.64; 'here': 0.66; 'skip:w 40': 0.68; 'safe': 0.72; 'eight': 0.74; 'email addr:hotmail.com': 0.89 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Trying to work with data from a query using Python. |
| Date | Fri, 07 Jun 2013 20:59 +0200 |
| Organization | None |
| References | <3f8d60b3-6b92-4670-ac99-28ec6fee580b@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084bc67.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2860.1370631552.3114.python-list@python.org> (permalink) |
| Lines | 91 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1370631552 news.xs4all.nl 15868 [2001:888:2000:d::a6]:34677 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:47348 |
Show key headers only | View raw
ethereal_robe@hotmail.com wrote:
> 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.
Keep it simple:
COLUMN_COUNT = 22
TEMPLATE = "04|85|{0}|||||{1}|||||||||||||||"
assert TEMPLATE.count("|") == COLUMN_COUNT -1, "You cannot count ;)"
for row in cur.fetchall():
print TEMPLATE.format(*row)
A bit more general:
fill_rows(rows):
out_row = [""] * 22
out_row[0] = "04"
out_row[1] = "85"
for row in rows:
out_row[2], out_row[7] = row
# copying not necessary here, but let's play it safe
yield out_row[:]
writer = csv.writer(sys.stdout, delimiter="|")
writer.writerows(fill_rows(cur.fetchall()))
All untested code.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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