当前位置:   article > 正文

python中csv文件的创建、读取等操作总结_使用vscode创建csv文件

使用vscode创建csv文件

python中csv文件的创建、读取等操作总结


CSV前言

CSV(逗号分隔值)格式是电子表格和数据库最常见的导入和导出格式,
它允许程序员说“以 Excel 首选的格式写入此数据”或“从 Excel 生成的此文件中读取数据”,而无需了解 Excel 使用的 CSV 格式的确切细节。

其中再wps以及微软的Excel这两个软件中,数据内容的格式是并不兼容的

  • wps中要用 utf-8 写入
  • Excel要用 utf-8-sig 写入

1. 导入csv库

当我们想要对CSV文件进行操作时,我们可以导入python自带的csv库进行操作

import csv
  • 1

2. CSV文件的写入操作

2.1 CSV文件的写入方法一:以列表方式写入

import csv

header = ['name', 'url', 'city']
data = [['才短思涩-求解', 'https://blog.csdn.net/caribbean666?type=blog', '保定']]
with open('ggg.csv', 'w', encoding='utf-8', newline='') as f:
    #创建文件
    write = csv.writer(f)
    # 单行写入
    write.writerow(header)
    # 多行写入
    write.writerows(data)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.2 CSV文件的写入方法二:以字典方式写入

import csv

header = ['name', 'url', 'city']
data = [{'name': '才短思涩-求解', 'url': 'https://blog.csdn.net/caribbean666?type=blog', 'city': '保定'}]
with open('dict.csv', 'w', encoding='utf-8', newline='') as f:
    #创建文件
    write = csv.DictWriter(f, header)
    #声明表头
    write.writeheader()
    # 单行写入
    # write.writerow(data)
    # 多行写入
    write.writerows(data)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

如果需要写入很多数据时只需要写入一次表头,那么可以再write.writeheader()加一个if判断

import csv
k=1
header = ['name','url','city']
data=[{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'}{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'}{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},{'name':'才短思涩-求解','url':'https://blog.csdn.net/caribbean666?type=blog','city':'保定'},]
with open('dict.csv','w',encoding='utf-8',newline='') as f:
   write=csv.DictWriter(f,header)
   # 声明表头
   if k==1:
      write.writeheader()
      k+=1
     # 单行写入
     # write.writerow(data)
     # 多行写入
   write.writerows(data)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3. CSV文件的读取操作

3.1 CSV文件的读取方法一:以列表方式读取

import csv

with open('dict.csv', 'r', encoding='utf-8') as f:
    #创建读取对象
    read = csv.reader(f)
    # print(read)# 生成器
    for i in read:
        print(i)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.2 CSV文件的读取方法二:以字典方式读取

import csv

with open('dict.csv', 'r', encoding='utf-8') as f:
    read = csv.DictReader(f) #生成器
    # print(read)
    for i in read:
        print(i)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意: 打开文件时应指定格式为w, 文本写入. 打开文件时,指定不自动添加新行newline=‘’,否则每写入一行就或多一个空行。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/43858
推荐阅读
相关标签
  

闽ICP备14008679号