Python2 常见问题
Python編程時遇到過一些問題,整理了以下內容,含解決辦法。
1、SyntaxError: Non-ASCII character ‘\xe4’ in file
文件中出現了中文,且沒有編碼聲明,Python2將默認以ASCII作為標準編碼,而Python2支持的ASCII碼無中文。
解決方法:
必須在文件中第一行聲明文件編碼
# -*- coding: utf-8 -*-2、UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range
此問題常見于Python2環境中。
解決方法:
import sys# Python2.x reload(sys) sys.setdefaultencoding("utf8")3、Python2寫文件中文亂碼
Python2中open方法是沒有encoding這個參數的,如果像python3一樣的寫法會報異常:
TypeError: ‘encoding’ is an invalid keyword argument for this function
解決方法:
4、Mac上PyCharm運行多進程報錯的解決方案
運行時報錯運行時報錯
may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.
解決方案
總結
以上是生活随笔為你收集整理的Python2 常见问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Frida基础操作命令
- 下一篇: Python 日志模块Loguru的使用