Wednesday, October 5, 2011

[Level 1] Encoding in Python script.

If there are some wording not for ascii, you should add # -*- coding: (encoding) -*-
to prevent compile error.
example error message:
File "./testEncoding.py", line 21
SyntaxError: Non-ASCII character '\xe6' in file ./testForLoop_2.6.py on line 21, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
e.g.
#!/usr/bin/python2.6
# -*- coding: utf8 -*-

### chinese testing
slang="有嘴說別人無嘴說自己"
sword="嘴"

## error with encoding
#for i in slang:
#  print i

# use python shell to display encoding
# ex.
# >>> "有嘴說別人無嘴說自己"
# '\xe6\x9c\x89\xe5\x98\xb4\xe8\xaa\xaa\xe5\x88\xa5\xe4\xba\xba\xef\xbc\x8c\xe7\x84\xa1\xe5\x98\xb4\xe8\xaa\xaa\xe8\x87\xaa\xe5\xb7\xb1\xe3\x80\x82'
# >>> "嘴"
# '\xe5\x98\xb4'

l=len(sword)
h=0
k=0
count=0
for item in slang:
  k=0
  f=1
  while k<l:
    if slang[h+k]==sword[k]:
      k+=1
      continue
    else:
      f=0
      break
  if f==1:
    count+=1
  h+=1
  if h>=(len(slang)-l):
    break
print count
$ ./testEncoding.py 
2
$
Wish this helps.
regards,
Stanley Huang