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编码的。因此读取数据的时候用了

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')



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM