python写入csv中文乱码问题

加入encoding='utf-8-sig'就不会乱码了
with open("haha.csv",'w',newline='',encoding='utf-8-sig') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["飞机转场记录号", "登机口"])

# test.csv代表文件路径;w代表只写入,a代表追加写入;encoding='utf-8-sig'解决写入乱码问题
with open(r'test\information.csv', 'w', newline='', encoding='utf-8-sig') as csvfile:
    writer = csv.writer(csvfile)  # 实例化writer对象
    # 创建表头信息
    header = ['小区名称', '地址', '建筑时间', '楼栋', '单元', '户室', '朝向', '面积']
    # 调用writerow方法写入一行表头信息,如果需要写入多行则调用writerows
    writer.writerow(header)

你可能感兴趣的:(Python基础,python,开发语言,后端)