python之文件操作,, 使用pytho


使用python进行文件操作

使用:

场景一: 逐行读取文件中的内容

filename = ‘sentiment_score.txt‘f = open(filename,‘r‘, encoding=‘UTF-8‘)line = f.readline()while line:    print(line, end=‘‘)    line = f.readline()f.close()

  常见问题:

    1.UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x80 in position

解决: 指定utf8字符集进行读取, 打开文件的时候,设置字符集 encoding=‘utf-8‘

python之文件操作

评论关闭