-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-troubleshooting
More file actions
39 lines (24 loc) · 1.4 KB
/
python-troubleshooting
File metadata and controls
39 lines (24 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Q:in python2.7,UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 0: invalid start byte:?
A:
Your string has a non ascii character encoded in it.
Not being able to decode with utf-8 may happen if you've needed to use other encodings in your code. For example:
>>> 'my weird character \x96'.decode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x96 in position 19: invalid start byte
In this case, the encoding is windows-1252 so you have to do:
>>> 'my weird character \x96'.decode('windows-1252')
u'my weird character \u2013'
Now that you have unicode, you can safely encode into utf-8.
python脚本crontab执行的几个坑。
坑一:
一般linux都预安装python,但是版本比较低。会出现python程序手工执行正常,但是用crontab执行报错,如缺少某某模块之类的。
最直接的解决办法:使用新版本python的绝对路径执行,如:
0 14 * * * /usr/local/bin/python2.7 /xxx.py > /yyy.log 2>&1 &
坑二:
如果python程序内使用到一些涉及环境变量的方法可能差异,如:
cfg = ConfigParser.ConfigParser()
cfg.read(r'/home/work/open-falcon/addin/sms.ini')
这里如果采用相对路径./sms.ini亦会导致无法正常读取。