读取文件lecture.txt
id lecturer 级别 工龄
1 liangquan B 10
2 moyi B 9
3 mengjian C 5
4 huangnan A 3
5 yanhao C 7
代码
import pandas as pd with open(r"D:/Edge Install/kettle/第七章/lecture.txt") as file_obj: df = pd.read_csv(file_obj, sep='\t') print(df.columns.tolist()) #遍历列行
结果出现中文乱码
['id', 'lecturer', '绾у埆', '宸ラ緞']
解决方法:在打开函数那里添加encoding="utf-8"
代码如下:
import pandas as pd with open(r"D:/Edge Install/kettle/第七章/lecture.txt",encoding="utf-8") as file_obj: df = pd.read_csv(file_obj, sep='\t') print(df.columns.tolist())