python 2.7版本解決TypeError: 'encoding' is an invalid keyword argument for this function

今天在用yaml處理數據時,因爲yaml.load可接收一個byte字符串,unicode字符串,打開的二進制文件或文本文件對象,但字節字符串和文件必須是utf-8,utf-16-be或utf-16-le編碼的。所以讀取數據的時候用了this

data_file = open("F:\\MyPro\\data.yaml", "r", encoding='utf-8')

運行的時候報錯:TypeError: 'encoding' is an invalid keyword argument for this function

網上查找一番後,改爲以下這樣就能夠搞定
import io
data_file = io.open("F:\\MyPro\\data.yaml", "r", encoding='utf-8')
相關文章
相關標籤/搜索