Python中出现的问题

1.TypeError: must be str, not bytes错误:

  解答: 写文件处 open(filename, 'w').write  应该写为 open(filename, 'wb').write

2.当文本文件里面有中文时,需要进行编码转换,(在网上查了很多都不行)

   with open("C://ch.js", encoding="utf-8") as data1: 
    for oneLine in data1: 
       print(oneLine) 

  编码转换:content = str(open(filepath).read(),'gbk').encode('utf8')

  当输出多一个b时加个decode(‘utf-8’)即可

转载于:https://www.cnblogs.com/gorgeous1314/archive/2012/07/31/2617081.html

猜你喜欢

转载自blog.csdn.net/weixin_34123613/article/details/93431937
今日推荐