只读 字符串 class log oop 文件 判断字符串 ()
使用 chardet 模块来判断数据的编码;输入参数为 str 类型。
- #coding:utf-8
- import cchardet
- f =open('hadoop.txt','r')#打开文本文件,只读about=f.read()#读取文本内容
- printcchardet.detect(about)#判断字符串编码
- #将字符串的原编码先转成unicode编码,再转成utf-8编码
- printabout.decode('gbk').encode('utf-8')
pd.DataFrame 数据类型转换
- # -*- coding: UTF-8 -*-
- import pandas as pd
- df = pd.DataFrame([{'col1':'a','col2':'1'}, {'col1':'b','col2':'2'}])
- print df
- print '查看df对象数据类型'
- print df.dtypes
- df['col2'] = df['col2'].astype('int')
- print '-------------------------------------------'
- printdf['col2'] .dtypes
- df['col2'] = df['col2'].astype('float64')
- print '-------------------------------------------'
- printdf.dtypes
当然方法还有很多。。。
python 编码及类型转换
来源: http://www.bubuko.com/infodetail-2086769.html