read_to
#python的pickle模块实现了基本的数据序列和反序列化。通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,
#永久存储;通过pickle模块的反序列化操作,我们能够从文件中创建上一次程序保存的对象。
总是会看到代码开头会加上from __future__ import *这样的语句。这样的做法的作用就是将新版本的特性引进当前版本中,也就是说我们可以在当前版本使用新版本的一些特性。
print用法:
# python 2.x
from __future__ import print_function
print("Hello World")
division用法
# python 2.x
5/2
>>> 2
from __future__ import division
5/2
>>> 2.5
with用法
# python 2.x
try:
with open('test.txt', 'w') as f:
f.write('Hello World')
finally:
f.close()
# 用with替代上述异常检测代码:
from __future__ import with_statement
with open('test.txt', 'w') as f:
f.write('Hi there!')
绝对引入
主要针对2.4版本之前
from __future__ import absolute_import
pandas读取csv文件
from __future__ import print_function
import pandas as pd
# Read from
data = pd.read_csv('C:\\Users\Public\\Documents\\python_study\\前置代码code\\code\\a1_python_lib\\1_pandas\\0_read_to\\student.csv',engine='python')
print('1')
print(data)
'''
Student ID name age gender
0 1100 Kelly 22 Female
1 1101 Clo 21 Female
2 1102 Tilly 22 Female
3 1103 Tony 24 Male
4 1104 David 20 Male
5 1105 Catty 22 Female
6 1106 M 3 Female
7 1107 N 43 Male
8 1108 A 13 Male
9 1109 S 12 Male
10 1110 David 33 Male
11 1111 Dw 3 Female
12 1112 Q 23 Male
13 1113 W 21 Female'''
保存文件为pickle格式
# Save to
data.to_pickle('C:\\Users\Public\\Documents\\python_study\\前置代码code\\code\\a1_python_lib\\1_pandas\\0_read_to\\student.pickle')
将pickle格式的文件加载出来
# Load pickle
import pprint, pickle
pkl_file = open('C:\\Users\Public\\Documents\\python_study\\前置代码code\\code\\a1_python_lib\\1_pandas\\0_read_to\\student.pickle', 'rb')
data1 = pickle.load(pkl_file)
print('2')
pprint.pprint(data1)
'''
Student ID name age gender
0 1100 Kelly 22 Female
1 1101 Clo 21 Female
2 1102 Tilly 22 Female
3 1103 Tony 24 Male
4 1104 David 20 Male
5 1105 Catty 22 Female
6 1106 M 3 Female
7 1107 N 43 Male
8 1108 A 13 Male
9 1109 S 12 Male
10 1110 David 33 Male
11 1111 Dw 3 Female
12 1112 Q 23 Male
13 1113 W 21 Female'''