1. 程式人生 > >python讀取utf-8檔案

python讀取utf-8檔案

在mac下python讀取utf-8編碼的txt檔案,如果檔案裡有中文,直接輸出的話會顯示成亂碼,也無法直接轉成utf-8,所以必須想將其轉成gbk,然後在轉成utf-8

# -*- coding: utf-8 -*-

import os


filename = open(".txt")

while True:

s = filename.readline()

if s:

s = s.decode('gbk')

s = s.encode('utf-8')

print s

else:

break