1、要添加以下语句
import sys
reload(sys)
sys.setdefaultencoding('utf-8)
2、示例程序
def main2(): dic = [] with codecs.open('***.txt', 'r', 'GBK') as f: lines = f.readlines() for line in lines: line = line.strip('\n') for word in line: if word == ' ': continue dic.append(str(word)) f1 = open('***.txt', 'a+') for i in range(len(dic)): s="'"+str(dic[i])+"'"+"," f1.write(s) f1.close() 要注意:有时候打开'GBK'可以,有时候'utf-8')可以