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