您的位置:首页 > 编程语言 > Python开发

使用python进行收据搜集示例之different_format_data_processing

2016-12-16 23:54 627 查看
这里是用jupyter notebook写的关于使用python进行数据收集的基本知识,包括crawl_and_parse、different_format_data_processing、feature_engineering_example和python_regular_expression等。之前课程里提供的资料,移植到了python3+windows环境上。代码上传到csdn资源啦:ABC of data_collection

为了方便查看,代码分开4篇博客里。

下面是jupyter notebook代码导出的md文件。

2.different_format_data_processing

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

实际的场景中,我们会在不同的地方遇到各种不同的数据格式(比如大家熟悉的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


‘C:\\Users\\NodYoung\\Desktop\\Python_Data_Analysis-master\\Python_Data_Analysis-master\\Lesson4_data_collection\\python3\\different_data_formats’

## 1.各式各样的文本数据

### 1.1 CSV与TXT读取

注意:windows下没有cat命令

# !cat data1.csv


df = pd.read_csv('data1.csv')
df


abcdmessage
01234hello
15678world
29101112foo
pd.read_table('data1.csv', sep=',')


abcdmessage
01234hello
15678world
29101112foo
# !cat data2.csv


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


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])


abcdmessage
01234hello
15678world
29101112foo
# !cat data5.csv
result = pd.read_csv('data5.csv')
print(result)
pd.isnull(result)


something a b c d message
0 one 1 2 3.0 4 NaN
1 two 5 6 NaN 8 world
2 three 9 10 11.0 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


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.sort_values(ascending=False)


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


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)
print(dates)
ts = Series(np.arange(7), index=dates)
print(ts)
ts.to_csv('tseries.csv')
# !cat tseries.csv


DatetimeIndex([‘2000-01-01’, ‘2000-01-02’, ‘2000-01-03’, ‘2000-01-04’,
‘2000-01-05’, ‘2000-01-06’, ‘2000-01-07’],
dtype=’datetime64[ns]’, freq=’D’)
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
Freq: D, dtype: int32

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


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:]
print(values)
data_dict = {h: v for h, v in zip(header, zip(*values))}
print(data_dict)


[[‘1’, ‘2’, ‘3’], [‘1’, ‘2’, ‘3’, ‘4’]]
{‘c’: (‘3’, ‘3’), ‘a’: (‘1’, ‘1’), ‘b’: (‘2’, ‘2’)}

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
pd.read_csv('mydata.csv')


one;two;three
01;2;3
14;5;6
27;8;9
### 1.5 JSON格式的数据

obj = \
"""
{"姓名": "张三",
"住处": ["天朝", "挖煤国", "万恶的资本主义日不落帝国"],
"宠物": null,
"兄弟": [{"姓名": "李四", "年龄": 25, "宠物": "汪星人"},
{"姓名": "王五", "年龄": 23, "宠物": "喵星人"}]
}
"""


import json
result = json.loads(obj)
result


{‘住处’: [‘天朝’, ‘挖煤国’, ‘万恶的资本主义日不落帝国’],
‘兄弟’: [{‘姓名’: ‘李四’, ‘宠物’: ‘汪星人’, ‘年龄’: 25},
{‘姓名’: ‘王五’, ‘宠物’: ‘喵星人’, ‘年龄’: 23}],
‘姓名’: ‘张三’,
‘宠物’: None}

print(json.dumps(result, ensure_ascii=False))


{“兄弟”: [{“年龄”: 25, “宠物”: “汪星人”, “姓名”: “李四”}, {“年龄”: 23, “宠物”: “喵星人”, “姓名”: “王五”}], “宠物”: null, “住处”: [“天朝”, “挖煤国”, “万恶的资本主义日不落帝国”], “姓名”: “张三”}

result[u"兄弟"][0]


{‘姓名’: ‘李四’, ‘宠物’: ‘汪星人’, ‘年龄’: 25}

print(json.dumps(result[u"兄弟"][0], 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 urllib.request import urlopen

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

doc = parsed.getroot()


doc


找到’’

links = doc.findall('.//a')
links[15:20]


[,
,
,
,
]

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/lucheng918’,
‘https://ask.julyedu.com/people/lucheng918’,
‘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)


135

def _unpack(spans):
return [val.text_content() for val in spans]


contents = _unpack(spans)
for content in contents:
print(content)


通知设置
贡献
回复了问题 • 2 人关注 • 1 个回复 • 5 次浏览 • 2 小时前
• 来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 3 次浏览 • 2 小时前
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 14 次浏览 • 6 小时前

• 来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 15 次浏览 • 1 天前
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 23 次浏览 • 1 天前

• 来自相关主题
贡献
回复了问题 • 1 人关注 • 2 个回复 • 21 次浏览 • 1 天前
• 来自相关主题
贡献
回复了问题 • 2 人关注 • 2 个回复 • 23 次浏览 • 1 天前
• 来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 35 次浏览 • 1 天前
• 来自相关主题
贡献
回复了问题 • 2 人关注 • 2 个回复 • 55 次浏览 • 2 天前
• 来自相关主题
贡献
回复了问题 • 3 人关注 • 2 个回复 • 84 次浏览 • 2 天前
• 来自相关主题
发起了问题 • 2 人关注 • 0 个回复 • 67 次浏览 • 3 天前

• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 63 次浏览 • 3 天前

• 来自相关主题
贡献
回复了问题 • 3 人关注 • 2 个回复 • 53 次浏览 • 3 天前
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 309 次浏览 • 3 天前

• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 71 次浏览 • 4 天前

• 来自相关主题
贡献
回复了问题 • 6 人关注 • 8 个回复 • 149 次浏览 • 5 天前
• 来自相关主题
发起了问题 • 2 人关注 • 0 个回复 • 49 次浏览 • 5 天前

• 来自相关主题
贡献
回复了问题 • 3 人关注 • 5 个回复 • 395 次浏览 • 5 天前
• 来自相关主题
贡献
回复了问题 • 34 人关注 • 16 个回复 • 4679 次浏览 • 6 天前
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 42 次浏览 • 6 天前

• 来自相关主题
发起了问题 • 3 人关注 • 0 个回复 • 78 次浏览 • 2016-12-07 18:41

• 来自相关主题
回复了问题 • 1 人关注 • 1 个回复 • 308 次浏览 • 2016-12-07 18:31
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 40 次浏览 • 2016-12-07 17:52

• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 39 次浏览 • 2016-12-07 17:37

• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 40 次浏览 • 2016-12-07 15:47

• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 55 次浏览 • 2016-12-06 22:39

• 来自相关主题
发起了问题 • 2 人关注 • 0 个回复 • 65 次浏览 • 2016-12-06 22:37

• 来自相关主题
贡献
回复了问题 • 2 人关注 • 2 个回复 • 63 次浏览 • 2016-12-06 19:37
• 来自相关主题
回复了问题 • 2 人关注 • 2 个回复 • 322 次浏览 • 2016-12-06 19:08
• 来自相关主题
贡献
回复了问题 • 1 人关注 • 2 个回复 • 66 次浏览 • 2016-12-06 17:55
• 来自相关主题
贡献
回复了问题 • 7 人关注 • 4 个回复 • 2726 次浏览 • 2016-12-06 17:48
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 44 次浏览 • 2016-12-06 11:42

• 来自相关主题
贡献
回复了问题 • 3 人关注 • 1 个回复 • 71 次浏览 • 2016-12-06 10:05
• 来自相关主题
贡献
回复了问题 • 3 人关注 • 2 个回复 • 84 次浏览 • 2016-12-05 11:23
• 来自相关主题
贡献
回复了问题 • 19 人关注 • 22 个回复 • 1221 次浏览 • 2016-12-04 21:22
• 来自相关主题
贡献
回复了问题 • 3 人关注 • 1 个回复 • 73 次浏览 • 2016-12-04 15:51
• 来自相关主题
贡献
回复了问题 • 3 人关注 • 2 个回复 • 63 次浏览 • 2016-12-04 00:20
• 来自相关主题
发起了问题 • 2 人关注 • 0 个回复 • 431 次浏览 • 2016-12-03 17:34

• 来自相关主题
贡献
回复了问题 • 2 人关注 • 3 个回复 • 91 次浏览 • 2016-12-03 11:38
• 来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 51 次浏览 • 2016-12-02 10:45
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 74 次浏览 • 2016-12-02 10:08

• 来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 58 次浏览 • 2016-12-01 22:16
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 68 次浏览 • 2016-12-01 10:47

• 来自相关主题
贡献
回复了问题 • 47 人关注 • 101 个回复 • 4228 次浏览 • 2016-12-01 10:00
• 来自相关主题
贡献
回复了问题 • 2 人关注 • 2 个回复 • 50 次浏览 • 2016-12-01 09:58
• 来自相关主题
贡献
回复了问题 • 4 人关注 • 6 个回复 • 373 次浏览 • 2016-11-30 21:56
• 来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 79 次浏览 • 2016-11-30 21:36
• 来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 43 次浏览 • 2016-11-30 21:30
• 来自相关主题
发起了问题 • 1 人关注 • 0 个回复 • 74 次浏览 • 2016-11-30 10:11

• 来自相关主题
贡献
回复了问题 • 2 人关注 • 1 个回复 • 95 次浏览 • 2016-11-29 15:59
• 来自相关主题
返回顶部

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)


《自动聊天机器人项目班》

七月在线app中下载的视频声音有杂音,听不清?

狂欢第三天:转盘直抽MacBook Air和锤子 M1

怎么分享活动页面呢

python爬虫报错 UnicodeEncodeError: ‘ascii’

量化课以及机器学习应用班的代码

七月在线双12三天大狂欢之第二天:转盘直播GTX 1080显卡和ipad mini4

进群核对名字没填咋办

How to buy some expired video classes from US?

七月在线双12三天大狂欢:转盘直抽iPhone 7、海量课程优惠券、全场课程限时特价秒杀

终于来了,《自然语言处理班》大纲讨论稿

今12.11之内抢购《Python数据分析班》,只需9元

人工神经网络输出至Softmax分类器的思考

mxnet examples 实验笔记(一)

今天12.10日之内,免费送《机器学习中的数学班》

希望能增加1-2倍的倍速播放

10月ML班第13讲作业 图模型(违反直觉的贝叶斯…)

《PHP与后端架构实战班》大纲讨论稿

一起刷Kaggle(数据挖掘、机器学习在线比赛)

训练好后, tensorflow哪个方法是根据一个input输出一个output的…

双12三天大狂欢:转盘抽奖iPhone 7、定时抢券、课程限时秒杀

Python网络爬虫实战项目代码大全

python,theano的typeerror调试

EM推导中的一个问题

python有木有把灰度图像转float的方法.

数学微课II:《矩阵与凸优化》大纲讨论稿

数学微课I:《概率论与数理统计》大纲讨论稿

《自动聊天机器人项目班》,期待啥内容

如何入门机器学习

EM中参数求解

Mac Neural Conversational Model 自动聊天机器人实验

傅盛:未来的公司本质都是数据公司

七月在线讲的到底怎么样

【11月深度学习第三讲】第二层卷积操作的对象是否将第一层的输出合并为一张图?

《spark 机器学习班》,你最想听到啥内容

GPU云租用或攒GPU机

我想问下想人工智能机器学习这些要求是不是很高

python 爬虫笔记(二):开源代理池,图片和社交网络数据的爬取

七月在线2016年度最火爆课程TOP 10

bagging分类时的概率计算问题

李开复:人工智能时代来临

云环境使用

tensorflow逻辑回归模型稀疏向量表示

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

tf实现2元线性回归

Python爬虫项目班,大纲讨论稿

七月在线即将推出线上数据大赛

七月在线总学员人数突破9000

1 元限时秒杀《动态规划实战班》,仅限今11.30日

寒老师您好,请问训练好的word2vec模型,然后怎们能够输入某个词,然后得到这个词的向量表示呢!多谢您!!

### 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)


from pandas import DataFrame

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


root.get('href')


root.text


## 二进制格式的数据

import pandas as pd

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


store['obj1']


abcdmessage
01234hello
15678world
29101112foo
import os
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


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


[(‘Atlanta’, ‘Georgia’, 1.25, 6),
(‘Tallahassee’, ‘Florida’, 2.6, 3),
(‘Sacramento’, ‘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=next(zip(*cursor.description)))


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 pymysql

conn= pymysql.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()


---------------------------------------------------------------------------

ConnectionRefusedError                    Traceback (most recent call last)

C:\Program Files\Anaconda3\lib\site-packages\pymysql\connections.py in connect(self, sock)
889                             sock = socket.create_connection(
--> 890                                 (self.host, self.port), self.connect_timeout)
891                             break

C:\Program Files\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address)
710     if err is not None:
--> 711         raise err
712     else:

C:\Program Files\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address)
701                 sock.bind(source_address)
--> 702             sock.connect(sa)
703             return sock

ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

OperationalError                          Traceback (most recent call last)

<ipython-input-46-1caebd09ae3e> in <module>()
7         user='root',
8         passwd='123456',
----> 9         db ='test',
10         )
11 cur = conn.cursor()

C:\Program Files\Anaconda3\lib\site-packages\pymysql\__init__.py in Connect(*args, **kwargs)
88     """
89     from .connections import Connection
---> 90     return Connection(*args, **kwargs)
91
92 from pymysql import connections as _orig_conn

C:\Program Files\Anaconda3\lib\site-packages\pymysql\connections.py in __init__(self, host, user, password, database, port, unix_socket, charset, sql_mode, read_default_file, conv, use_unicode, client_flag, cursorclass, init_command, connect_timeout, ssl, read_default_group, compress, named_pipe, no_delay, autocommit, db, passwd, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout)
686             self._sock = None
687         else:
--> 688             self.connect()
689
690     def _create_ssl_ctx(self, sslp):

C:\Program Files\Anaconda3\lib\site-packages\pymysql\connections.py in connect(self, sock)
935                 exc.traceback = traceback.format_exc()
936                 if DEBUG: print(exc.traceback)
--> 937                 raise exc
938
939             # If e is neither DatabaseError or IOError, It's a bug.

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([WinError 10061] 由于目标计算机积极拒绝,无法连接。)")


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-48-1c58d1aac9bf> 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())


---------------------------------------------------------------------------

ImportError                               Traceback (most recent call last)

<ipython-input-49-7bcb5fe4f264> in <module>()
1
----> 2 import pymongo
3
4 connection=pymongo.Connection('10.32.38.50',27017)
5

ImportError: No module named 'pymongo'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐