python.pandas如何读写含有中文的文件-dat文件怎么打开

python.pandas如何读写含有中文的文件-dat文件怎么打开

源代码(以python2.7版本为例):

#!/usr/bin/env python
#coding:utf-8
import pandas as pd
data=pd.read_csv("F:/py/student.csv")
print 'data:'
print data
data2=pd.read_csv(u"F:/py/student二.csv")#打开含有中文的文件路径
print 'data2:'
print data2
data3=pd.read_csv("F:/py/student3.csv",encoding="gb2312")#打开含有中文的文件
print 'data3:'
print data3
data.to_pickle("F:/py/student.pickle")#存储为pickle形式的文件,注:pickle是python自带的一个文件格式,它的读写速度要比csv快,机器学习中常常用到
data2.to_pickle(u"F:/py/student二.pickle")#存储为pickle形式的文件,该文件路径含有中文
data3.to_csv("F:/py/student32.csv",encoding="gb2312")#存储为csv形式的文件,该文件含有中文
data4=pd.read_pickle("F:/py/student.pickle")
print 'data4:'
print data4

运行结果:

C:\Users\lhy001\PycharmProjects\\untitled2\venv\Scripts\python.exe F:/py/pandastest3.py

data:

Student ID name age gender

0 1100 Abe 22 Female

1 1101 Bob 25 Female

2 1102 David 23 Female

3 1103 Kimi 21 Male

4 1104 Sakura 22 Male

5 1105 Nihonsan 22 Female

6 1106 Candy 21 Female

7 1107 France 23 Male

8 1108 Pchy 24 Female

data2:

Student ID name age gender

0 1109 Noh 24 Female

1 1110 Eivn 23 Male

2 1111 Lemon 21 Female

3 1112 Boby 23 Male

4 1113 Andy 22 Male

5 1114 Band 22 Female

6 1115 Janer 21 Male

7 1116 David 23 Male

8 1117 Coco 23 Female

data3:

Student ID name age gender

0 1100 爱贝 22 Female

1 1101 波波 25 Female

2 1102 大伟 23 Female

3 1103 凯米 21 Male

4 1104 樱 22 Male

5 1105 霓虹君 22 Female

6 1106 凯迪 21 Female

7 1107 弗兰斯 23 Male

8 1108 皮 24 Female

data4:

Student ID name age gender

0 1100 Abe 22 Female

1 1101 Bob 25 Female

2 1102 David 23 Female

3 1103 Kimi 21 Male

4 1104 Sakura 22 Male

5 1105 Nihonsan 22 Female

6 1106 Candy 21 Female

7 1107 France 23 Male

8 1108 Pchy 24 Female

Process finished with exit code 0

推荐阅读