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')
发布了91 篇原创文章 · 获赞 47 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_30007885/article/details/102399264