2013-06-18 26 views
0

我查询使用psycopg2是混合列名与根据表中的值选择

c.execute("select * from train_temp") 
trans=np.array(c.fetchall()) 

,坐落于预期的数据我有一行与列名的整个Postgres的表。

trans[-1,] 
Out[63]: 
array(['ACTION', 'RESOURCE', 'MGR_ID', 'ROLE_ROLLUP_1', 'ROLE_ROLLUP_2', 
     'ROLE_DEPTNAME', 'ROLE_TITLE', 'ROLE_FAMILY_DESC', 'ROLE_FAMILY', 
     'ROLE_CODE', None, None, None, None, None, None, None, None, None], dtype=object) 

更令人费解的是行数返回表中的

trans.shape 
Out[67]: (32770, 19) 



select count(1) from train_temp ; 
count 
------- 
32770 
(1 row) 

这里匹配行的数量来是很表的

      Table "public.train_temp" 
     Column  |  Type  | Modifiers | Storage | Description 
---------------------+------------------+-----------+----------+------------- 
action    | text    |   | extended | 
resource   | text    |   | extended | 
mgr_id    | text    |   | extended | 
role_rollup_1  | text    |   | extended | 
role_rollup_2  | text    |   | extended | 
role_deptname  | text    |   | extended | 
role_title   | text    |   | extended | 
role_family_desc | text    |   | extended | 
role_family   | text    |   | extended | 
role_code   | text    |   | extended | 
av_role_code  | double precision |   | plain | 
av_role_family  | double precision |   | plain | 
av_role_family_desc | double precision |   | plain | 
av_role_title  | double precision |   | plain | 
av_role_deptname | double precision |   | plain | 
av_role_rollup_2 | double precision |   | plain | 
av_role_rollup_1 | double precision |   | plain | 
av_mgr_id   | double precision |   | plain | 
av_resource   | double precision |   | plain | 
Has OIDs: no 

这是怎么回事架构?请注意,它不会发生在所有表格中。其实对于这最后一个过程中正常工作

Table "public.play" 
    Column |  Type  | Modifiers | Storage | Description 
-----------+------------------+-----------+----------+------------- 
row.names | text    |   | extended | 
action | double precision |   | plain | 
color  | text    |   | extended | 
type  | text    |   | extended | 
Has OIDs: no 

这最后一个表作为字符串完全通过,而问题的一个尊重的数据类型。

play[1,] 
Out[73]: 
array(['2', '0.0', 'blue', 'car'], 
     dtype='|S5') 


trans[1,] 
Out[74]: 
array(['1', '0', '36', '117961', '118413', '119968', '118321', '117906', 
     '290919', '118322', 0.920412992041299, 0.942349726775956, 
     0.933439675174014, 0.920412992041299, 0.976, 0.964478764478764, 
     0.949222217031812, 0.909090909090909, 0.923076923076923], dtype=object) 

感谢您的见解。

+0

如果你已经解决了你的情况,不要把在的问题。将其张贴为答案,然后将您的答案标记为已接受。 – doppelgreener

回答

0

其实我只是在将* csv导入到postgres时自己写了标题。

我应该用header选项psql这样

\copy test from 'test.csv' with (delimiter ',' , format csv, header TRUE); 
相关问题