Read a lot of information on the net, previously did not understand Encode,decode before Uncode,utf-8, now understand, Uncode is transcoding internal intermediary, such as
#Coding:utf-8a='China'Printrepr (a)#at this point A is a utf8 byte stream.B=a.decode ('UTF8')Printrepr (b)#B is the Uncode string.#because the encoding shown on the Windows,cmd console is only GBK or uncode, it cannot be displayed UTF8#we need to convert the UTF8 to GBK when we write the console program.C=b.encode ('GBK')Printrepr (c)PrintaPrintbPrintC
Decode is the decoding of other encoded strings to Uncode,encode is to encode uncode encoded strings into other encoded strings for saving or displaying, Uncode is an internal intermediary
In the interactive shell (Idlle) There is a problem is that under Windows, A=u ' China ' is, will be this u will be copied as is, such as, but written into the script to run, there is no problem, and found that the version without B pymssql also has this problem, Read the GBK string will be no reason to add u but the code has not changed some input method inputs are also a problem, will be inexplicably many characters in.
Python 2.7.9 (default, Dec, 12:28:03) [MSC v.1500 64bit (AMD64)] on Win32type"Copyright","credits" or "license ()" forMore information.>>> A=u'in'>>>au'\xd6\xd0'>>> a='in'>>>a'\xd6\xd0'>>>
Here is a small program written today based on the number of log statistics:
#Coding:utf-8#Count Jiebang times in log#a successful Delivery group message appears in the file as a successful solutionImportOS fromTimeImportSleeplogpath='C:\log.log' #define the Unbind log pathfile=open (LogPath,'R')#Open the Unbind file as read-only to generate the Unbind file objectA=u'Send Group message succeeded' #initialize a string of judgmentsCountline=0#Initialize total number of rowsCountjiebang=0#number of initial dissolve bindings while1: Line=file.readline ()#Read Unbind if notLine Break #If you go to the end of the file, jump #Print LineCountline+=1ifAinchLine.decode ('GBK'): #because it is used to GBK code, so to decode #Determine If this row contains a judgment string #Print 1 #测试用Countjiebang+=1#Sleep (2) #测试用File.close ()#Close File ObjectPrintrepr (a)Print 'lines:', Countline#shows how many lines are in totalPrint 'Jiebang times:', Countjiebang#show how many times the solution was tiedOs.system ('Pause')
Log.log Fragment:
2015-02-09 18:23:46 Information unbind : 1652392404|13122700501|13 unbind: 12015-02-09 18:24:58 messages Send SMS Center heartbeat success. 2015-02-09 18:25:03 Information unbind the stack |15661614231| 2015-02-09 18:25:03, Baoshan District, Shanghai City Information Group message: 41897886| ( 931010138) | message: Unbind 15661614231 2015-02-09 18:25:06 Information Group: 1545325917 Send Group Message success: "15661614231" in Shanghai Baoshan District successfully untied!
Thank you: http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html This blog post, some of the online direct sweep down the article will confuse people.
About python27 Chinese Code