– 关注前端和node js,分享福利和心得!

python数据保存为csv

保存list数据

  • 1、引入csv
    import csv
  • 2、创建文件对象
    headers=["姓名","性别"]
    data = [
    {'name':'张三', 'sex':'男'},
    {'name':'李四', 'sex':'女'}
    ]
    f=open("user.csv","w",encoding="utf-8")
    writer=csv.writer(f,headers)
  • 3、写入表头
    writer.writerow(headers)
  • 4、写入行内容
    writer.writerows(result)
  • 5、简写
    headers=["姓名","性别"]
    data = [
    {'name':'张三', 'sex':'男'},
    {'name':'李四', 'sex':'女'}
    ]
    with open("book.csv","w",encoding="utf-8") as f:
    writer=csv.writer(f,headers)
    writer.writerow(headers)
    writer.writerows(data)
    

最新文章

    热门文章