how to make return(self.res) not to output the content of list ?

Peter Otten __peter__ at web.de
Wed Sep 24 12:33:56 EDT 2014


luofeiyu wrote:

> There is a file named analyse.py in the D:\Python34\Lib\site-packages.
> 
> 
> import sqlite3,os,jinja2
> db = r'F:\workspace\china\data\china.sqlite'
> con = sqlite3.connect(db)
> cur = con.cursor()
> 
> class status():
>      def grow(self):
>          self.res=cur.execute('select 代码,所属行业,注册资本,雇员人数,管
> 理人员人数 from profile limit 10').fetchall()
> 
>      def display(self):
>          template = jinja2.Template('''
>          <table border=1>
>          {% for row in res %}
>              <tr>
>              {% for col in row -%}
>                  <td>{{ col}}</td>
>              {% endfor %}
>              </tr>
>          {% endfor %}
>          </table>
>          ''')
>          with open('f:\\test.html', 'w') as f:
>               f.write(template.render(res=self.res))
>          import webbrowser
>          webbrowser.open('f:\\test.html')
> 
> 
> when i open python3.4  console to input  the code:
> import analyse
> x=analyse.status()
> x.grow()
> x.display()
> 
> i get
> 
> 
> when i add  return(self.res) in grow method to make analyse.py into the
> following:
> 
> import sqlite3,os,jinja2
> db = r'F:\workspace\china\data\china.sqlite'
> con = sqlite3.connect(db)
> cur = con.cursor()
> 
> class status():
>      def grow(self):
>          self.res=cur.execute('select 代码,所属行业,注册资本,雇员人数,管
> 理人员人数 from profile limit 10').fetchall()
> return(self.res)
>      def display(self):
>          template = jinja2.Template('''
>          <table border=1>
>          {% for row in res %}
>              <tr>
>              {% for col in row -%}
>                  <td>{{ col}}</td>
>              {% endfor %}
>              </tr>
>          {% endfor %}
>          </table>
>          ''')
>          with open('f:\\test.html', 'w') as f:
>               f.write(template.render(res=self.res))
>          import webbrowser
>          webbrowser.open('f:\\test.html')
> 
> 
> now again input the code:
> import analyse
> x=analyse.status()
> x.grow()
> the x.grow() will output
> 
> [('600000', '银行', '187亿', 39340.0, 30.0),
> ('600004', '民航机场', '11.5亿', 4499.0, 23.0),
>   ('600005', '钢铁行业', '101亿', 38857.0, 24.0),
> ('600006', '汽车行业', '20.0亿', 10290.0, 20.0),
> ('600007', '房地产', '10.1亿', 2332.0, 19.0),
>   ('600008', '公用事业', '22.0亿', 6515.0, 20.0),
> ('600009', '民航机场', '19.3亿', 5472.0, 18.0),
> ('600010', '钢铁行业', '80.0亿', 31389.0, 19.0),
> ('600011', '电力行业', '141亿', 37729.0, 29.0),
> ('600012', '高速公路', '16.6亿', 2106.0, 14.0)]
> 
> now what i want to do is :
> 1.keep  return(self.res)  in grow method.
> 2.it is my target that when run  the code:
> 
> import analyse
> x=analyse.status()
> x.grow()
> 
> there is nothing output in my console ,  to make return(self.res) not to
> output the content of list ,
> how can i do ?

You can set the display hook:

>>> [1, 2, 3]
[1, 2, 3]
>>> import sys
>>> sys.displayhook = lambda obj: None
>>> [1, 2, 3]
>>> 






More information about the Python-list mailing list