python多种格式数据加载、处理与存储

多种格式数据加载、处理与存储

实际的场景中,我们会在不同的地方遇到各种不同的数据格式(比如大家熟悉的csv与txt,比如网页HTML格式,比如XML格式),我们来一起看看python如何和这些格式的数据打交道。

2016-08

from __future__ import division
from numpy.random import randn
import numpy as np
import os
import sys
import matplotlib.pyplot as plt
np.random.seed(12345)
plt.rc('figure', figsize=(10, 6))
from pandas import Series, DataFrame
import pandas as pd
np.set_printoptions(precision=4)
%pwd
u'/Users/zzy/GitHub/jupter-project/lesson_4_ipython_notebooks/different_data_formats'

1.各式各样的文本数据

1.1 CSV与TXT读取

!cat data1.csv
a,b,c,d,message
1,2,3,4,hello
5,6,7,8,world
9,10,11,12,foo
df = pd.read_csv('data1.csv')
df
abcdmessage
01234hello
15678world
29101112foo
pd.read_table('data1.csv', sep=',')
abcdmessage
01234hello
15678world
29101112foo
!cat data2.csv
1,2,3,4,hello
5,6,7,8,world
9,10,11,12,foo
pd.read_csv('data2.csv', header=None)
01234
01234hello
15678world
29101112foo
pd.read_csv('data2.csv', names=['a', 'b', 'c', 'd', 'message'])
abcdmessage
01234hello
15678world
29101112foo
names = ['a', 'b', 'c', 'd', 'message']
pd.read_csv('data2.csv', names=names, index_col='message')
abcd
message
hello1234
world5678
foo9101112
!cat csv_mindex.csv
parsed = pd.read_csv('csv_mindex.csv', index_col=['key1', 'key2'])
parsed
key1,key2,value1,value2
one,a,1,2
one,b,3,4
one,c,5,6
one,d,7,8
two,a,9,10
two,b,11,12
two,c,13,14
two,d,15,16
value1value2
key1key2
onea12
b34
c56
d78
twoa910
b1112
c1314
d1516
list(open('data3.txt'))
['            A         B         C\n',
 'aaa -0.264438 -1.026059 -0.619500\n',
 'bbb  0.927272  0.302904 -0.032399\n',
 'ccc -0.264273 -0.386314 -0.217601\n',
 'ddd -0.871858 -0.348382  1.100491\n']
result = pd.read_table('data3.txt', sep='\s+')
result
ABC
aaa-0.264438-1.026059-0.619500
bbb0.9272720.302904-0.032399
ccc-0.264273-0.386314-0.217601
ddd-0.871858-0.3483821.100491
!cat data4.csv
pd.read_csv('data4.csv', skiprows=[0, 2, 3])
# hey!
a,b,c,d,message
# just wanted to make things more difficult for you
# who reads CSV files with computers, anyway?
1,2,3,4,hello
5,6,7,8,world
9,10,11,12,foo
abcdmessage
01234hello
15678world
29101112foo
!cat data5.csv
result = pd.read_csv('data5.csv')
result
pd.isnull(result)
something,a,b,c,d,message
one,1,2,3,4,NA
two,5,6,,8,world
three,9,10,11,12,foo
somethingabcdmessage
0FalseFalseFalseFalseFalseTrue
1FalseFalseFalseTrueFalseFalse
2FalseFalseFalseFalseFalseFalse
result = pd.read_csv('data5.csv', na_values=['NULL'])
result
somethingabcdmessage
0one123.04NaN
1two56NaN8world
2three91011.012foo
sentinels = {'message': ['foo', 'NA'], 'something': ['two']}
pd.read_csv('data5.csv', na_values=sentinels)
somethingabcdmessage
0one123.04NaN
1NaN56NaN8world
2three91011.012NaN

1.2 分片/块读取文本数据

result = pd.read_csv('data6.csv')
result
onetwothreefourkey
00.467976-0.038649-0.295344-1.824726L
1-0.3588931.4044530.704965-0.200638B
2-0.5018400.659254-0.421691-0.057688G
30.2048861.0741341.388361-0.982404R
40.354628-0.1331160.283763-0.837063Q
51.8174800.7422730.419395-2.251035Q
6-0.7767640.935518-0.332872-1.875641U
7-0.9131351.530624-0.5726570.477252K
80.358480-0.497572-0.3670160.507702S
9-1.740877-1.160417-1.6378302.172201G
100.240564-0.3282491.2521551.0727968
110.7640181.165476-0.6395441.495258R
120.571035-0.3105370.582437-0.2987651
132.3176580.430710-1.3342160.199679P
141.547771-1.119753-2.2776340.329586J
15-1.3106080.401719-1.0009871.156708E
16-0.0884960.6347120.1533240.415335B
17-0.018663-0.247487-1.4465220.750938A
18-0.070127-1.5790970.1208920.671432F
19-0.194678-0.4920392.3596050.319810H
20-0.2486180.868707-0.492226-0.717959W
21-1.091549-0.867110-0.647760-0.832562C
220.641404-0.138822-0.621963-0.284839C
231.2164080.9926870.165162-0.069619V
24-0.5644740.7928320.7470530.571675I
251.759879-0.515666-0.2304811.362317S
260.1262660.3092810.382820-0.239199L
271.334360-0.100152-0.840731-0.6439676
28-0.7376200.278087-0.053235-0.950972J
29-1.148486-0.986292-0.1449630.124362Y
..................
99700.633495-0.1865240.9276270.1431644
99710.308636-0.1128570.762842-1.0729771
9972-1.627051-0.9781510.154745-1.229037Z
99730.3148470.0979890.1996080.955193P
99741.6669070.9920050.496128-0.686391S
99750.0106030.708540-1.2587110.226541K
99760.118693-0.714455-0.501342-0.254764K
99770.302616-2.011527-0.6280850.768827H
9978-0.0985721.769086-0.215027-0.053076A
9979-0.0190581.9649940.738538-0.883776F
9980-0.5953490.001781-1.423355-1.458477M
99811.392170-1.396560-1.425306-0.847535H
9982-0.896029-0.1522871.9244830.3651846
9983-2.274642-0.9018741.5003520.996541N
9984-0.3018981.0199061.1021602.624526I
9985-2.548389-0.5853741.496201-0.718815D
9986-0.0645880.759292-1.568415-0.420933E
9987-0.143365-1.111760-1.8155810.4352742
9988-0.070412-1.0559210.338017-0.440763X
99890.6491480.994273-1.3842270.485120Q
9990-0.3707690.404356-1.051628-1.0508998
9991-0.4099800.155627-0.8189901.277350W
99920.301214-1.1112030.6682580.671922A
99931.8211170.4164450.1738740.505118X
99940.0688041.3227590.8023460.223618H
99952.311896-0.417070-1.409599-0.515821L
9996-0.479893-0.6504190.745152-0.646038E
99970.5233310.7871120.4860661.093156K
9998-0.3625590.598894-1.8432010.887292G
9999-0.096376-1.012999-0.657431-0.5733150

10000 rows × 5 columns

pd.read_csv('data6.csv', nrows=5)
onetwothreefourkey
00.467976-0.038649-0.295344-1.824726L
1-0.3588931.4044530.704965-0.200638B
2-0.5018400.659254-0.421691-0.057688G
30.2048861.0741341.388361-0.982404R
40.354628-0.1331160.283763-0.837063Q
chunker = pd.read_csv('data6.csv', chunksize=100)
chunker
<pandas.io.parsers.TextFileReader at 0x10d3b5950>
chunker = pd.read_csv('data6.csv', chunksize=100)

tot = Series([])
for piece in chunker:
    tot = tot.add(piece['key'].value_counts(), fill_value=0)

tot = tot.order(ascending=False)
/Library/Python/2.7/site-packages/ipykernel/__main__.py:7: FutureWarning: order is deprecated, use sort_values(...)
tot[:10]
E    368.0
X    364.0
L    346.0
O    343.0
Q    340.0
M    338.0
J    337.0
F    335.0
K    334.0
H    330.0
dtype: float64

1.3 把数据写入文本格式

data = pd.read_csv('data5.csv')
data
somethingabcdmessage
0one123.04NaN
1two56NaN8world
2three91011.012foo
data.to_csv('out.csv')
!cat out.csv
,something,a,b,c,d,message
0,one,1,2,3.0,4,
1,two,5,6,,8,world
2,three,9,10,11.0,12,foo
data.to_csv(sys.stdout, sep='|')
|something|a|b|c|d|message
0|one|1|2|3.0|4|
1|two|5|6||8|world
2|three|9|10|11.0|12|foo
data.to_csv(sys.stdout, na_rep='NULL')
,something,a,b,c,d,message
0,one,1,2,3.0,4,NULL
1,two,5,6,NULL,8,world
2,three,9,10,11.0,12,foo
data.to_csv(sys.stdout, index=False, header=False)
one,1,2,3.0,4,
two,5,6,,8,world
three,9,10,11.0,12,foo
data.to_csv(sys.stdout, index=False, columns=['a', 'b', 'c'])
a,b,c
1,2,3.0
5,6,
9,10,11.0
dates = pd.date_range('1/1/2000', periods=7)
ts = Series(np.arange(7), index=dates)
ts.to_csv('tseries.csv')
!cat tseries.csv
2000-01-01,0
2000-01-02,1
2000-01-03,2
2000-01-04,3
2000-01-05,4
2000-01-06,5
2000-01-07,6
Series.from_csv('tseries.csv', parse_dates=True)
2000-01-01    0
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
dtype: int64

1.4 手动读写数据(按要求)

!cat data7.csv
"a","b","c"
"1","2","3"
"1","2","3","4"
import csv
f = open('data7.csv')

reader = csv.reader(f)
for line in reader:
    print(line)
['a', 'b', 'c']
['1', '2', '3']
['1', '2', '3', '4']
lines = list(csv.reader(open('data7.csv')))
header, values = lines[0], lines[1:]
data_dict = {h: v for h, v in zip(header, zip(*values))}
data_dict
{'a': ('1', '1'), 'b': ('2', '2'), 'c': ('3', '3')}
class my_dialect(csv.Dialect):
    lineterminator = '\n'
    delimiter = ';'
    quotechar = '"'
    quoting = csv.QUOTE_MINIMAL
with open('mydata.csv', 'w') as f:
    writer = csv.writer(f, dialect=my_dialect)
    writer.writerow(('one', 'two', 'three'))
    writer.writerow(('1', '2', '3'))
    writer.writerow(('4', '5', '6'))
    writer.writerow(('7', '8', '9'))
%cat mydata.csv
one;two;three
1;2;3
4;5;6
7;8;9

1.5 JSON格式的数据

obj = \
"""
{"姓名": "张三",
 "住处": ["天朝", "挖煤国", "万恶的资本主义日不落帝国"],
 "宠物": null,
 "兄弟": [{"姓名": "李四", "年龄": 25, "宠物": "汪星人"},
              {"姓名": "王五", "年龄": 23, "宠物": "喵星人"}]
}
"""
import json
result = json.loads(obj)
result
{u'\u4f4f\u5904': [u'\u5929\u671d',
  u'\u6316\u7164\u56fd',
  u'\u4e07\u6076\u7684\u8d44\u672c\u4e3b\u4e49\u65e5\u4e0d\u843d\u5e1d\u56fd'],
 u'\u5144\u5f1f': [{u'\u59d3\u540d': u'\u674e\u56db',
   u'\u5ba0\u7269': u'\u6c6a\u661f\u4eba',
   u'\u5e74\u9f84': 25},
  {u'\u59d3\u540d': u'\u738b\u4e94',
   u'\u5ba0\u7269': u'\u55b5\u661f\u4eba',
   u'\u5e74\u9f84': 23}],
 u'\u59d3\u540d': u'\u5f20\u4e09',
 u'\u5ba0\u7269': None}
print json.dumps(result, encoding="UTF-8", ensure_ascii=False)
{"兄弟": [{"年龄": 25, "宠物": "汪星人", "姓名": "李四"}, {"年龄": 23, "宠物": "喵星人", "姓名": "王五"}], "住处": ["天朝", "挖煤国", "万恶的资本主义日不落帝国"], "宠物": null, "姓名": "张三"}
result[u"兄弟"][0]
{u'\u59d3\u540d': u'\u674e\u56db',
 u'\u5ba0\u7269': u'\u6c6a\u661f\u4eba',
 u'\u5e74\u9f84': 25}
print json.dumps(result[u"兄弟"][0], encoding="UTF-8", ensure_ascii=False)
{"年龄": 25, "宠物": "汪星人", "姓名": "李四"}
asjson = json.dumps(result)
brothers = DataFrame(result[u'兄弟'], columns=[u'姓名', u'年龄'])
brothers
姓名年龄
0李四25
1王五23

1.6 人人都爱爬虫,人人都要解析XML 和 HTML

from lxml.html import parse
from urllib2 import urlopen

parsed = parse(urlopen('https://ask.julyedu.com/'))

doc = parsed.getroot()
doc
<Element html at 0x1092ed100>
links = doc.findall('.//a')
links[15:20]
[<Element a at 0x1091afcb0>,
 <Element a at 0x1091afd08>,
 <Element a at 0x1091afd60>,
 <Element a at 0x1091afdb8>,
 <Element a at 0x1091afe10>]
lnk = links[14]
lnk
lnk.get('href')
print lnk.text_content()
全部问题
urls = [lnk.get('href') for lnk in doc.findall('.//a')]
urls[-10:]
['https://ask.julyedu.com/people/July',
 'https://ask.julyedu.com/people/July',
 'http://weibo.com/askjulyedu',
 None,
 'https://www.julyedu.com/help/index/about',
 'https://www.julyedu.com/help/index/contact',
 'https://www.julyedu.com/help/index/join',
 'http://ask.julyedu.com/question/55',
 'http://www.julyapp.com',
 'http://www.miitbeian.gov.cn/']
spans = doc.findall('.//span')
len(spans)
137
def _unpack(spans):
    return [val.text_content() for val in spans]
contents = _unpack(spans)
for content in contents:
    print content
 通知设置
贡献
回复了问题 • 2 人关注 • 1 个回复 • 22 次浏览 • 2 小时前              
 •  来自相关主题
发起了问题 • 2 人关注 • 0 个回复 • 21 次浏览 • 22 小时前 
                
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 17 次浏览 • 1 天前 
                
 •  来自相关主题
贡献
回复了问题 • 3 人关注 • 1 个回复 • 38 次浏览 • 1 天前               
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 35 次浏览 • 1 天前 
                
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 29 次浏览 • 2 天前 
                
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 31 次浏览 • 2 天前 
                
 •  来自相关主题
贡献
回复了问题 • 37 人关注 • 36 个回复 • 484 次浏览 • 1 天前                
 •  来自相关主题
贡献
回复了问题 • 12 人关注 • 17 个回复 • 795 次浏览 • 2 天前                
 •  来自相关主题
贡献
回复了问题 • 4 人关注 • 4 个回复 • 197 次浏览 • 3 天前              
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 44 次浏览 • 3 天前               
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 34 次浏览 • 3 天前               
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 37 次浏览 • 3 天前               
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 102 次浏览 • 3 天前              
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 39 次浏览 • 4 天前               
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 26 次浏览 • 4 天前 
                
 •  来自相关主题
贡献
回复了问题 • 3 人关注 • 2 个回复 • 126 次浏览 • 4 天前              
 •  来自相关主题
发表了文章 • 1 个评论 • 764 次浏览 • 4 天前
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 49 次浏览 • 5 天前 
                
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 35 次浏览 • 5 天前 
                
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 59 次浏览 • 6 天前 
                
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 42 次浏览 • 6 天前 
                
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 55 次浏览 • 6 天前               
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 44 次浏览 • 6 天前               
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 42 次浏览 • 2016-08-28 16:12 
                
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 42 次浏览 • 2016-08-28 15:47 
                
 •  来自相关主题
贡献
回复了问题 • 4 人关注 • 1 个回复 • 530 次浏览 • 2016-08-28 14:48              
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 3 个回复 • 911 次浏览 • 2016-08-27 19:18              
 •  来自相关主题
贡献
回复了问题 • 39 人关注 • 82 个回复 • 3670 次浏览 • 2016-08-27 11:59               
 •  来自相关主题
贡献
回复了问题 • 4 人关注 • 6 个回复 • 639 次浏览 • 2016-08-28 14:32              
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 77 次浏览 • 2016-08-25 14:13 
                
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 274 次浏览 • 2016-08-25 10:37              
 •  来自相关主题
回复了问题 • 1 人关注 • 1 个回复 • 48 次浏览 • 2016-08-24 17:20               
 •  来自相关主题
贡献
回复了问题 • 11 人关注 • 14 个回复 • 1052 次浏览 • 2016-08-24 10:01               
 •  来自相关主题
发起了问题 • 2 人关注 • 0 个回复 • 76 次浏览 • 2016-08-23 15:16 
                
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 62 次浏览 • 2016-08-22 21:51               
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 2 个回复 • 584 次浏览 • 2016-08-22 12:26              
 •  来自相关主题
回复了问题 • 1 人关注 • 1 个回复 • 78 次浏览 • 2016-08-21 18:57               
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 62 次浏览 • 2016-08-21 18:25               
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 105 次浏览 • 2016-08-21 15:17              
 •  来自相关主题
贡献
回复了问题 • 3 人关注 • 2 个回复 • 749 次浏览 • 2016-08-21 11:16              
 •  来自相关主题
贡献
回复了问题 • 5 人关注 • 4 个回复 • 434 次浏览 • 2016-08-19 16:07              
 •  来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 69 次浏览 • 2016-08-19 15:55               
 •  来自相关主题
回复了问题 • 1 人关注 • 1 个回复 • 108 次浏览 • 2016-08-19 14:48              
 •  来自相关主题
回复了问题 • 2 人关注 • 1 个回复 • 168 次浏览 • 2016-08-19 14:54              
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 79 次浏览 • 2016-08-19 10:59 
                
 •  来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 96 次浏览 • 2016-08-18 19:04 
                
 •  来自相关主题
贡献
回复了问题 • 9 人关注 • 5 个回复 • 864 次浏览 • 2016-08-18 18:41              
 •  来自相关主题
贡献
回复了问题 • 3 人关注 • 1 个回复 • 1005 次浏览 • 2016-08-18 14:01             
 •  来自相关主题
贡献
回复了问题 • 7 人关注 • 12 个回复 • 619 次浏览 • 2016-08-18 12:05             
 •  来自相关主题

                            python
                        

                            leetcode
                        
返回顶部

                    var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1259748782'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s11.cnzz.com/z_stat.php%3Fid%3D1259748782%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));
                

                        
                        
                    

                        
                        
                    

                        
                        
                    
questions = doc.findall('.//h4')
len(questions)
50
contents = _unpack(questions)
for content in contents:
    print content
                        @寒老师,CTR课程中的LR-MLlib的代码哪里下载?
                    

                        逻辑回归如何做特征选择?
                    

                        opencv在vs2015配置失败,有朋友能给一些可用的教程吗
                    

                        数据集具有倾斜性或呈长尾分布问题
                    

                        端到端深度学习在自动驾驶汽车上的应用
                    

                        CV班:转发微博狂送100元上课券,所有人 人人都有份
                    

                        关于word2vec的一些资料
                    

                        python数据分析班作业1
                    

                        第8类新班:《计算机视觉班》大纲讨论稿
                    

                        有奖竞答第二题(IBM ponder this 专题):国王、硬币与电子秤的故事
                    

                        一个数学问题
                    

                        关于推荐系统基于内容的推荐中使用TF-IDF建模词对资料重要度问题
                    

                        python数据分析班-安装igraph后运行报错
                    

                        anaconda程序组中的各个组件都是做什么用的
                    

                        wide&deep learning问题
                    

                        SMO算法中关于b值的求解
                    

                        caffe车辆检测
                    

                        2017年各大互联网公司校招日程统计
                    

                        应用班第8次课 图像检索作业
                    

                        大事记:官网上线自动支付、七月在线APP全面开放下载
                    

                        趋势科技南京研发中心招聘机器学习、数据挖掘人才
                    

                        CNN 在文本中的应用
                    

                        关于特征工程的一些问题
                    

                        【DNN】为什么神经网络需要激励函数
                    

                        关于python的一个问题
                    

                        conda和pip install各种package有什么区别
                    

                        《python数据分析班》环境安装
                    

                        16年8月内推公司名单:VIPKID(算法负责人)、溢思得瑞(数据科学家)、洋钱罐(数据挖掘)
                    

                        一起来吐槽——对官网跟问答社区,有任何反馈或意见?
                    

                        关于EM算法的一些疑问
                    

                        deep learning做文本分类
                    

                        关于特征工程的三个问题
                    

                        python 3 读取数据编码报错 跪求解决
                    

                        有奖竞答第一题:LR和线性回归的区别和联系有哪些?
                    

                        创新工场涂鸦移动团队2017校园招聘
                    

                        python 安装问题谁能帮一下忙?
                    

                        PCA本质理解问题
                    

                        用softmax做输出层碰到的问题
                    

                        最近做图像花图或者屏幕花屏检测,请问特征提取这块有没有什么思路或者方法
                    

                        【内推】【美团点评】2017届校招内推开始了
                    

                        Scince上发表的聚类算法,C++编程实现,有一个bug不知道怎么解决!!跪求大神!!!
                    

                        求e^x ,为什么不直接使用泰勒展开式?
                    

                        自动聊天机器人
                    

                        一般完整机器学习项目的工作流程
                    

                        如何入门机器学习
                    

                        搭建一个自动驾驶车模
                    

                        “Google TensorFlow 深度学习笔记”
                    

                        海量数据处理问题
                    

                        关于  弱监督学习
                    

                        《Python数据分析班》,已超过400人火爆报名
                    

1.7 解析XML

!head -21 Performance_MNR.xml
from lxml import objectify

path = 'Performance_MNR.xml'
parsed = objectify.parse(open(path))
root = parsed.getroot()
data = []

skip_fields = ['PARENT_SEQ', 'INDICATOR_SEQ',
               'DESIRED_CHANGE', 'DECIMAL_PLACES']

for elt in root.INDICATOR:
    el_data = {}
    for child in elt.getchildren():
        if child.tag in skip_fields:
            continue
        el_data[child.tag] = child.pyval
    data.append(el_data)
perf = DataFrame(data)
perf
AGENCY_NAMECATEGORYDESCRIPTIONFREQUENCYINDICATOR_NAMEINDICATOR_UNITMONTHLY_ACTUALMONTHLY_TARGETPERIOD_MONTHPERIOD_YEARYTD_ACTUALYTD_TARGET
0Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.9951200896.995
1Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%9595220089695
2Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.9953200896.395
3Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%98.3954200896.895
4Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%95.8955200896.695
5Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%94.4956200896.295
6Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96957200896.295
7Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.4958200896.295
8Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%93.7959200895.995
9Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.4951020089695
10Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.99511200896.195
11Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%95.1951220089695
12Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%92.696.21200992.696.2
13Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.896.22200994.696.2
14Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.996.23200995.496.2
15Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.196.24200995.996.2
16Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.896.25200996.296.2
17Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.396.26200996.496.2
18Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.796.27200996.596.2
19Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%95.796.28200996.496.2
20Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.196.29200996.396.2
21Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%94.896.210200996.296.2
22Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%95.796.211200996.196.2
23Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%9596.21220099696.2
24Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%9896.3120109896.3
25Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%9396.32201095.696.3
26Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.996.33201096.196.3
27Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%98.196.34201096.696.3
28Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.696.35201096.896.3
29Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.496.36201096.996.3
.......................................
618Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%947200995.14
619Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%978200995.38
620Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%98.39200995.7
621Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%98.710200996
622Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%98.111200996.21
623Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%10012200996.5
624Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%97.95971201097.9597
625Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100972201098.9297
626Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100973201099.2997
627Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100974201099.4797
628Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100975201099.5897
629Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%91.21976201098.1997
630Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100977201098.4697
631Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100978201098.6997
632Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%95.2979201098.397
633Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%90.919710201097.5597
634Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%96.679711201097.4797
635Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%90.039712201096.8497
636Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100971201110097
637Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100972201110097
638Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%97.07973201198.8697
639Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%98.18974201198.7697
640Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%79.18975201190.9197
641Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%976201197
642Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%977201197
643Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%978201197
644Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%979201197
645Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%9710201197
646Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%9711201197
647Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%9712201197

648 rows × 12 columns

root
<Element PERFORMANCE at 0x108a0f290>
root.get('href')
root.text

二进制格式的数据

frame = pd.read_csv('data1.csv')
frame
frame.to_pickle('frame_pickle')
pd.read_pickle('frame_pickle')
abcdmessage
01234hello
15678world
29101112foo

使用HDF5格式

store = pd.HDFStore('mydata.h5')
store['obj1'] = frame
store['obj1_col'] = frame['a']
store
<class 'pandas.io.pytables.HDFStore'>
File path: mydata.h5
/obj1                frame        (shape->[3,5])
/obj1_col            series       (shape->[3])  
store['obj1']
store.close()
os.remove('mydata.h5')

HTML与API交互

import requests
url = 'https://api.github.com/repos/pydata/pandas/milestones/28/labels'
resp = requests.get(url)
resp
<Response [200]>
data[:5]
[{'AGENCY_NAME': 'Metro-North Railroad',
  'CATEGORY': 'Service Indicators',
  'DESCRIPTION': 'Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n',
  'FREQUENCY': 'M',
  'INDICATOR_NAME': 'On-Time Performance (West of Hudson)',
  'INDICATOR_UNIT': '%',
  'MONTHLY_ACTUAL': 96.9,
  'MONTHLY_TARGET': 95.0,
  'PERIOD_MONTH': 1,
  'PERIOD_YEAR': 2008,
  'YTD_ACTUAL': 96.9,
  'YTD_TARGET': 95.0},
 {'AGENCY_NAME': 'Metro-North Railroad',
  'CATEGORY': 'Service Indicators',
  'DESCRIPTION': 'Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n',
  'FREQUENCY': 'M',
  'INDICATOR_NAME': 'On-Time Performance (West of Hudson)',
  'INDICATOR_UNIT': '%',
  'MONTHLY_ACTUAL': 95.0,
  'MONTHLY_TARGET': 95.0,
  'PERIOD_MONTH': 2,
  'PERIOD_YEAR': 2008,
  'YTD_ACTUAL': 96.0,
  'YTD_TARGET': 95.0},
 {'AGENCY_NAME': 'Metro-North Railroad',
  'CATEGORY': 'Service Indicators',
  'DESCRIPTION': 'Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n',
  'FREQUENCY': 'M',
  'INDICATOR_NAME': 'On-Time Performance (West of Hudson)',
  'INDICATOR_UNIT': '%',
  'MONTHLY_ACTUAL': 96.9,
  'MONTHLY_TARGET': 95.0,
  'PERIOD_MONTH': 3,
  'PERIOD_YEAR': 2008,
  'YTD_ACTUAL': 96.3,
  'YTD_TARGET': 95.0},
 {'AGENCY_NAME': 'Metro-North Railroad',
  'CATEGORY': 'Service Indicators',
  'DESCRIPTION': 'Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n',
  'FREQUENCY': 'M',
  'INDICATOR_NAME': 'On-Time Performance (West of Hudson)',
  'INDICATOR_UNIT': '%',
  'MONTHLY_ACTUAL': 98.3,
  'MONTHLY_TARGET': 95.0,
  'PERIOD_MONTH': 4,
  'PERIOD_YEAR': 2008,
  'YTD_ACTUAL': 96.8,
  'YTD_TARGET': 95.0},
 {'AGENCY_NAME': 'Metro-North Railroad',
  'CATEGORY': 'Service Indicators',
  'DESCRIPTION': 'Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n',
  'FREQUENCY': 'M',
  'INDICATOR_NAME': 'On-Time Performance (West of Hudson)',
  'INDICATOR_UNIT': '%',
  'MONTHLY_ACTUAL': 95.8,
  'MONTHLY_TARGET': 95.0,
  'PERIOD_MONTH': 5,
  'PERIOD_YEAR': 2008,
  'YTD_ACTUAL': 96.6,
  'YTD_TARGET': 95.0}]
issue_labels = DataFrame(data)
issue_labels
AGENCY_NAMECATEGORYDESCRIPTIONFREQUENCYINDICATOR_NAMEINDICATOR_UNITMONTHLY_ACTUALMONTHLY_TARGETPERIOD_MONTHPERIOD_YEARYTD_ACTUALYTD_TARGET
0Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.9951200896.995
1Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%9595220089695
2Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.9953200896.395
3Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%98.3954200896.895
4Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%95.8955200896.695
5Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%94.4956200896.295
6Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96957200896.295
7Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.4958200896.295
8Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%93.7959200895.995
9Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.4951020089695
10Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.99511200896.195
11Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%95.1951220089695
12Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%92.696.21200992.696.2
13Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.896.22200994.696.2
14Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.996.23200995.496.2
15Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.196.24200995.996.2
16Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.896.25200996.296.2
17Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.396.26200996.496.2
18Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.796.27200996.596.2
19Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%95.796.28200996.496.2
20Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.196.29200996.396.2
21Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%94.896.210200996.296.2
22Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%95.796.211200996.196.2
23Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%9596.21220099696.2
24Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%9896.3120109896.3
25Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%9396.32201095.696.3
26Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%96.996.33201096.196.3
27Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%98.196.34201096.696.3
28Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.696.35201096.896.3
29Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei...MOn-Time Performance (West of Hudson)%97.496.36201096.996.3
.......................................
618Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%947200995.14
619Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%978200995.38
620Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%98.39200995.7
621Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%98.710200996
622Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%98.111200996.21
623Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%10012200996.5
624Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%97.95971201097.9597
625Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100972201098.9297
626Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100973201099.2997
627Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100974201099.4797
628Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100975201099.5897
629Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%91.21976201098.1997
630Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100977201098.4697
631Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100978201098.6997
632Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%95.2979201098.397
633Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%90.919710201097.5597
634Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%96.679711201097.4797
635Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%90.039712201096.8497
636Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100971201110097
637Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%100972201110097
638Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%97.07973201198.8697
639Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%98.18974201198.7697
640Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%79.18975201190.9197
641Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%976201197
642Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%977201197
643Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%978201197
644Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%979201197
645Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%9710201197
646Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%9711201197
647Metro-North RailroadService IndicatorsPercent of the time that escalators are operat...MEscalator Availability%9712201197

648 rows × 12 columns

2.数据库相关操作

2.1 sqlite数据库

import sqlite3

query = """
CREATE TABLE test
(a VARCHAR(20), b VARCHAR(20),
 c REAL,        d INTEGER
);"""

con = sqlite3.connect(':memory:')
con.execute(query)
con.commit()
data = [('Atlanta', 'Georgia', 1.25, 6),
        ('Tallahassee', 'Florida', 2.6, 3),
        ('Sacramento', 'California', 1.7, 5)]
stmt = "INSERT INTO test VALUES(?, ?, ?, ?)"

con.executemany(stmt, data)
con.commit()
cursor = con.execute('select * from test')
rows = cursor.fetchall()
rows
[(u'Atlanta', u'Georgia', 1.25, 6),
 (u'Tallahassee', u'Florida', 2.6, 3),
 (u'Sacramento', u'California', 1.7, 5)]
cursor.description
(('a', None, None, None, None, None, None),
 ('b', None, None, None, None, None, None),
 ('c', None, None, None, None, None, None),
 ('d', None, None, None, None, None, None))
DataFrame(rows, columns=zip(*cursor.description)[0])
abcd
0AtlantaGeorgia1.256
1TallahasseeFlorida2.603
2SacramentoCalifornia1.705
import pandas.io.sql as sql
sql.read_sql('select * from test', con)
abcd
0AtlantaGeorgia1.256
1TallahasseeFlorida2.603
2SacramentoCalifornia1.705

3.2 MySQL数据库

#coding=utf-8
import MySQLdb

conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
cur = conn.cursor()

#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

#插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")


#修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")

#删除查询条件的数据
#cur.execute("delete from student where age='9'")

cur.close()
conn.commit()
conn.close()

3.3 Memcache

#coding:utf8
import memcache

class MemcachedClient():
    ''' python memcached 客户端操作示例 '''

    def __init__(self, hostList):
        self.__mc = memcache.Client(hostList);

    def set(self, key, value):
        result = self.__mc.set("name", "NieYong")
        return result

    def get(self, key):
        name = self.__mc.get("name")
        return name

    def delete(self, key):
        result = self.__mc.delete("name")
        return result

if __name__ == '__main__':
    mc = MemcachedClient(["127.0.0.1:11511", "127.0.0.1:11512"])
    key = "name"
    result = mc.set(key, "NieYong")
    print "set的结果:", result
    name = mc.get(key)
    print "get的结果:", name
    result = mc.delete(key)
    print "delete的结果:", result
---------------------------------------------------------------------------

ImportError                               Traceback (most recent call last)

<ipython-input-39-51dc19a879b8> in <module>()
      1 
----> 2 import memcache
      3 
      4 class MemcachedClient():
      5     ''' python memcached 客户端操作示例 '''


ImportError: No module named memcache

3.4 MongoDB

#encoding:utf=8  
import pymongo  
  
connection=pymongo.Connection('10.32.38.50',27017)  
  
#选择myblog库  
db=connection.myblog  
  
# 使用users集合  
collection=db.users  
  
# 添加单条数据到集合中  
user = {"name":"cui","age":"10"}  
collection.insert(user)  
  
#同时添加多条数据到集合中  
users=[{"name":"cui","age":"9"},{"name":"cui","age":"11"}]  
collection.insert(users)  
  
#查询单条记录  
print collection.find_one()  
  
#查询所有记录  
for data in collection.find():  
    print data  
  
#查询此集合中数据条数  
print collection.count()  
  
#简单参数查询  
for data in collection.find({"name":"1"}):  
    print data  
  
#使用find_one获取一条记录  
print collection.find_one({"name":"1"})  
  
  
#高级查询  
print "__________________________________________"  
print '''''collection.find({"age":{"$gt":"10"}})'''  
print "__________________________________________"  
for data in collection.find({"age":{"$gt":"10"}}).sort("age"):  
    print data  
  
# 查看db下的所有集合  
print db.collection_names()  

转载于:https://www.cnblogs.com/chaoren399/p/5956384.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值