我有一个Python脚本来处理包含报告使用信息的.txt文件.我想找到一种方法来使用pprint的pprint(vars(object))函数干净地打印对象的属性.
该脚本读取文件并创建Report类的实例.这是班级.
class Report(object):
def __init__(self, line, headers):
self.date_added=get_column_by_header(line,headers,"Date Added")
self.user=get_column_by_header(line,headers,"Login ID")
self.report=get_column_by_header(line,headers,"Search/Report Description")
self.price=get_column_by_header(line,headers,"Price")
self.retail_price=get_column_by_header(line,headers,"Retail Price")
def __str__(self):
from pprint import pprint
return str(pprint(vars(self)))
Run Code Online (Sandbox Code Playgroud)
我希望能够干净地打印报告的实例a-la-pprint.
for i,line in enumerate(open(path+file_1,'r')):
line=line.strip().split("|")
if i==0:
headers=line
if i==1:
record=Report(line,headers)
print record
Run Code Online (Sandbox Code Playgroud)
我打电话的时候
print record
Run Code Online (Sandbox Code Playgroud)
对于单个Report实例,这是我在shell中得到的.
{'date_added': '1/3/2012 14:06',
'price': '0',
'report': 'some_report',
'retail_price': '0.25',
'user': 'some_username'}
None
Run Code Online (Sandbox Code Playgroud)
我的问题是双重的.
首先,这是一种干净地打印对象属性的好/期望方式吗?有或没有pprint有更好的方法吗?
第二,为什么呢
None
Run Code Online (Sandbox Code Playgroud)
最后打印到shell?我很困惑那来自哪里.
谢谢你的任何提示.
我最近升级了我的熊猫版本.我现在安装了最新的稳定版本:
pd.__version__
Out[5]: '0.10.1'
Run Code Online (Sandbox Code Playgroud)
在此升级之前,这是数据框在qtconsole shell中的显示方式(这不是我的屏幕截图,而只是我在网上找到的一个).
最新版本的pandas还使用不同的方法来设置显示选项.
而不是使用pd.set_printoptions
,熊猫希望你使用这样的set_option
配置:
pd.set_option('display.notebook_repr_html', True)
Run Code Online (Sandbox Code Playgroud)
升级我的pandas版本后,qtconsole不再将数据帧呈现为html表.
一个例子:
import numpy as np
import pandas as pd
pd.set_option('display.notebook_repr_html', True)
pd.set_option('display.expand_frame_repr', True)
pd.set_option('display.precision', 3)
pd.set_option('display.line_width', 100)
pd.set_option('display.max_rows', 50)
pd.set_option('display.max_columns', 10)
pd.set_option('display.max_colwidth', 15)
Run Code Online (Sandbox Code Playgroud)
当我创建一个DataFrame时......
f = lambda x: x*np.random.rand()
data = {"a": pd.Series(np.arange(10) ** 2 ),
"b": pd.Series(map(f, np.ones(10))) }
df = pd.DataFrame(data)
df
Run Code Online (Sandbox Code Playgroud)
这是我在qtconsole shell中看到的:
Out[4]:
a b
0 0 0.15
1 1 0.74
2 4 0.81
3 9 0.94
4 16 0.40
5 …
Run Code Online (Sandbox Code Playgroud) 我有一个64位的anaconda python发行版2.3,在Windows 7机器上安装了python 3.4.3.我搜索了关于在此之上安装rodeo但似乎"conda install rodeo"不会工作,所以我做了"pip install rodeo".
"pip install rodeo" gave me the following message "Successfully installed rodeo".
Run Code Online (Sandbox Code Playgroud)
但是当我在cmd中键入rodeo以启动rodeo时,它会给出一个错误说法
"failed to create process."
Run Code Online (Sandbox Code Playgroud)
我无法开始牛仔竞技表演.
请指教.
谢谢
我试图弄清楚如何返回每组Trans.TranSID的前10个记录.
SELECT a.ABID, a.ABName, t.TranSID, SUM(IIF(TranTypeID = 'CO', td.Qty * CAST(td.Price AS money) * - 1,
td.Qty * CAST(td.Price AS money))) AS TotalSales
FROM Trans t INNER JOIN
TransDetail td ON t.TranID = td.TranID INNER JOIN
ABook a ON t.TranABID = a.ABID
WHERE (t.TranDate BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-01-01 00:00:00', 102)) AND
t.TranTypeID in ('SO','CA','CO') AND (t.TranStatus <> 'V')
GROUP BY a.ABID, a.ABName, t.TranSID
HAVING (NOT (a.ABName LIKE '%cash%'))
ORDER BY t.TranSID, TotalSales Desc
Run Code Online (Sandbox Code Playgroud)
我可以在select语句中添加"TOP 10",但不管是哪个组,这都会给我前10个帐号.有25组Trans.TranSID,我试图获得每组的前10名.