Python encoding and type conversion

Source: Internet
Author: User

Use the Chardet module to determine the encoding of the data, and the input parameter is the STR type.

#Coding:utf-8ImportCCHARDETF=open ('Hadoop.txt','R')#Open Text file, read-onlyAbout=f.read ()#reading text contentPrintCchardet.detect (about)#Determining string encoding

#converts the original encoding of a string to Unicode encoding and then to UTF-8 encodingPrintAbout.decode ('GBK'). Encode ('Utf-8')

Pd. Dataframe Data type Conversions

#-*-coding:utf-8-*-ImportPandas as Pddf= PD. DataFrame ([{'col1':'a','col2':'1'}, {'col1':'b','col2':'2'}])PrintDFPrint 'viewing DF Object data Types'Printdf.dtypesdf['col2'] = df['col2'].astype ('int')Print '-------------------------------------------'Printdf['col2']. dtypesdf['col2'] = df['col2'].astype ('float64')Print '-------------------------------------------'PrintDf.dtypes

Of course there are a lot of ways ...

Python encoding and type conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.