日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python2 常见问题

發布時間:2024/4/11 python 80 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

解決方法:

# -*- coding: utf-8 -*-import iotest_1 = "中文" with io.open("test.txt", "w", encoding="utf-8") as f:f.write(unicode(test_1, "utf-8"))with open("test.txt", "r") as f:test_2 = unicode(f.read(), "utf-8")print test_2

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.

解決方案

添加環境變量:點擊窗口上的Run->Edit Configurations...->Environment variables->點擊輸入欄后的文件夾圖標添加內容:key: OBJC_DISABLE_INITIALIZE_FORK_SAFETY, value: YES完整示例:OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES

總結

以上是生活随笔為你收集整理的Python2 常见问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。