Convert Chinese characters to pinyin in Python

Source: Internet
Author: User

python3.4 environment, in the version below 3.0, some places are different, can be modified by themselves.

All articles can be converted, the only flaw is that punctuation is normal and cannot be converted. Think about making changes later.

1 #CODING=GBK2 " "3 Created on 2014-8-294 5 @author: Administrator6 " "7 #Import OS8 9 defCONVERT (CH):Ten     """the function returns its phonetic alphabet by entering Chinese characters, and if multiple characters are entered, the first Chinese pinyin is returned. One If you enter a numeric string, or if you enter an English letter, it returns itself (if the letter is uppercase, it is converted to lowercase) A     """ -length = Len ('ke')#Test Chinese characters take up bytes, Utf-8, Chinese characters occupy 3 bytes. bg2312, Chinese characters occupy 2 bytes -Intord = Ord (ch[0:1]) the     if(Intord >= 48 andIntord <= 57): -         returnCh[0:1] -     if(Intord >= 65 andIntord <=90)or(Intord >= 97 andIntord <=122): -         returnCh[0:1].lower () +ch = ch[0:len (CH)]#multiple kanji only get the first one -With open (r'C:\\convert-utf-8.txt') as F: +          forLineinchF: A             ifChinchLine : at                 returnLine[length:len (line)-2] -  -Path ='c:\\test\\. txt' -f =Open (Path) -p =F.read () -  forIinchP: in     Print(Convert (i), end= ")

The Convert-utf-8.txt in the code is a conversion file that can be downloaded in my web disk.

As for the Shen. txt inside is a Chinese and digital combination, testing, you can DIY, hey.

Network address: Http://pan.baidu.com/s/1eQGLDaq

Convert Chinese characters to pinyin in Python

Related Article

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.