Python报错解决方法总结
2016-01-26 00:00
706 查看
摘要: 记录在使用Python过程中出错及解决错误的方法,欢迎各位多提意见。
1.1 使用json.loads(json_data)时,出现:
出现错误的原因是字符串中包含了回车符(\r)或者换行符(\n)
解决方法:
(1)对这些字符转义:
(2)使用关键字strict:
strict默认是True,它将严格控制内部字符串,将其设置为False,便可以允许你\n \r。
出错原因是Python2.7在安装时,默认的编码是ascii,当程序中出现非ascii编码时,Python的处理常常会报这样的错,不过在Python3中就不会有这样的问题。
解决方法:
(1)临时解决方法:
在代码前加入:
(1)一劳永逸:
在Python的lib\site-packages文件夹下新建一个sitecustomize.py,内容如下:
这样的话,系统在Python启动的时候,自行调用该文件,设置系统的默认编码。
1、ValueError: Invalid control character at: line 1 column 8363 (char 8362)
1.1 使用json.loads(json_data)时,出现:
ValueError: Invalid control character at: line 1 column 8363 (char 8362)
出现错误的原因是字符串中包含了回车符(\r)或者换行符(\n)
解决方法:
(1)对这些字符转义:
json_data = json_data.replace('\r', '\\r').replace('\n', '\\n')
(2)使用关键字strict:
json.loads(json_data, strict=False)
strict默认是True,它将严格控制内部字符串,将其设置为False,便可以允许你\n \r。
2、UnicodeEncodeError: ascii codec can’t encode异常错误
2.1 在windows下写的python脚本,放到linux下运行,直接报
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128)
出错原因是Python2.7在安装时,默认的编码是ascii,当程序中出现非ascii编码时,Python的处理常常会报这样的错,不过在Python3中就不会有这样的问题。
解决方法:
(1)临时解决方法:
在代码前加入:
import sys reload(sys) sys.setdefaultencoding('utf8')
(1)一劳永逸:
在Python的lib\site-packages文件夹下新建一个sitecustomize.py,内容如下:
# encoding=utf8 import sys reload(sys) sys.setdefaultencoding('utf8')
这样的话,系统在Python启动的时候,自行调用该文件,设置系统的默认编码。
相关文章推荐
- python入门学习之变量篇
- python学习笔记四 迭代器,生成器,装饰器(基础篇)
- Python数据分析学习路径图(120天Get新技能)
- python 之readability与BeautifulSoup
- windows XP下Python2.7包管理工具安装-setuptool,pip、distribute、nose、virtualenv
- 将文件树归档到一个压缩的tar文件中
- python flask介绍
- windows下面安装Python和pip终极教程
- python之路-前端相关
- Python 6.1 错误处理
- 基于python的爬虫
- python numpy的使用
- Python分词统计
- Python遥感数据主成分分析
- python打印等腰三角形
- 让python在hadoop上跑起来
- python下文件的批量复制
- Python基本代码
- Python -- 装饰器
- python小白-day4迭代器和生成器