经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
Python中CSV文件(逗号分割)实战操作指南
来源:jb51  时间:2022/7/4 14:09:32  对本文有异议

一、csv文件介绍

1、csv文件简介

逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。通常都是纯文本文件。

2、为什么要使用csv文件

在Linux中我们可以通过命令在数据库中把表导出来为csv结尾的文件,其实就是以逗号分割分txt文件,此文件我们可以在windows中打开并且为表格的形式,方便我们进行查看与再次操作。

eg:

  1. MariaDB [test]> select * from 表名 into outfile "/tmp/test.csv" fields terminated by ",";

二、csv文件查看

注意:这里我是把csv文件和python代码都放在同级目录,否则要指定路径!!!

1、测试文件创建

(1)这里我们以windows中的csv文件来做实验

(2)我们可以选中内容复制进去,也可以上传到linux中,这里我们选择前者

  1. [root@python _test]# vim test.csv ###可以发现复制进去的是以空格为分隔符
  2. id username passwd age
  3. 1 dream1 123 21
  4. 2 dream2 456 22
  5. 3 dream3 789 23
  6.  
  7. ### 把空格替换为逗号
  8. [root@python _test]# sed -i 's/\s\+/,/g' test.csv

2、查看csv文件(列表)

(1)读出结果

  1. [root@python _test]# vim _test.py
  2. #!/usr/bin/env python
  3. #coding:utf-8
  4. import csv
  5. with open('test.csv', encoding="utf-8") as f:
  6. reader = csv.reader(f)
  7. print(reader)
  8. print(list(reader))
  9.  
  10. ### 查看结果
  11. [root@python _test]# python _test.py
  12. <_csv.reader object at 0x7f54d9a01eb8>
  13. [['id', 'username', 'passwd', 'age'], ['1', 'dream1', '123', '21'], ['2', 'dream2', '456', '22'], ['3', 'dream3', '789', '23']]

(2)遍历(从第一行读取)

  1. [root@python _test]# vim _test.py
  2. #!/usr/bin/env python
  3. #coding:utf-8
  4. import csv
  5. with open('test.csv', encoding="utf-8") as f:
  6. reader = csv.reader(f)
  7. for i in reader:
  8. print(reader.line_num, i)
  9. ### 查看结果
  10. [root@python _test]# python _test.py
  11. 1 ['id', 'username', 'passwd', 'age']
  12. 2 ['1', 'dream1', '123', '21']
  13. 3 ['2', 'dream2', '456', '22']
  14. 4 ['3', 'dream3', '789', '23']

(2)遍历(从第二行读取)

  1. [root@python _test]# vim _test.py
  2. #!/usr/bin/env python
  3. #coding:utf-8
  4. import csv
  5. with open('test.csv', encoding="utf-8") as f:
  6. reader = csv.reader(f)
  7. ### 这个就是我们得表头
  8. next(reader)
  9. for i in reader:
  10. print(reader.line_num, i)
  11. ### 查看结果
  12. [root@python _test]# python _test.py
  13. 2 ['1', 'dream1', '123', '21']
  14. 3 ['2', 'dream2', '456', '22']
  15. 4 ['3', 'dream3', '789', '23']

3、查看csv文件(字典)

(1)查看

  1. [root@python _test]# vim _test.py
  2. #!/usr/bin/env python
  3. #coding:utf-8
  4. import csv
  5. with open('test.csv', encoding="utf-8") as f:
  6. reader = csv.DictReader(f)
  7. ### 表头
  8. print (reader.fieldnames)
  9. print (reader,type(reader))
  10. for i in reader:
  11. print (i)
  12. ### 查看结果
  13. [root@python _test]# python _test.py
  14. ['id', 'username', 'passwd', 'age']
  15. <csv.DictReader object at 0x7f3b02213a20> <class 'csv.DictReader'>
  16. OrderedDict([('id', '1'), ('username', 'dream1'), ('passwd', '123'), ('age', '21')])
  17. OrderedDict([('id', '2'), ('username', 'dream2'), ('passwd', '456'), ('age', '22')])
  18. OrderedDict([('id', '3'), ('username', 'dream3'), ('passwd', '789'), ('age', '23')])

(2)查看第一列(id)

优点:我们不知道表头在具体那列,我们可以通过表头名来获取整列数据,即我们可以随便调整顺序也不会影响我们的数据读取!!!

  1. [root@python _test]# vim _test.py
  2. #!/usr/bin/env python
  3. #coding:utf-8
  4. import csv
  5. with open('test.csv', encoding="utf-8") as f:
  6. reader = csv.DictReader(f)
  7. for i in reader:
  8. print (i['id'])
  9. ### 查看结果
  10. [root@python _test]# python _test.py
  11. 1
  12. 2
  13. 3

4、写入文件(列表)

  1. [root@python _test]# vim _test.py
  2. #!/usr/bin/env python
  3. #coding:utf-8
  4. import csv
  5. li = [["id","user","性别"],["1","dreamya1","男"],["2","dreamya2","女"]]
  6. with open('user.csv', 'w', newline='') as f:
  7. writer = csv.writer(f)
  8. for i in li:
  9. writer.writerow(i)
  10.  
  11. ### 查看结果
  12. [root@python _test]# python _test.py
  13. [root@python _test]# cat user.csv
  14. id,user,性别
  15. 1,dreamya1,男
  16. 2,dreamya2,女

下载到windows中查看:

  1. [root@python _test]# sz user.csv

5、写入文件(字典)

  1. [root@python _test]# vim _test.py
  2. import csv
  3. #coding:utf-8
  4. headers = ['id', 'username','passwd']
  5. li = [{'id':'1','username':'dream1','passwd':'123'},
  6. {'id':'2','username':'dream2','passwd':'456'},
  7. ]
  8. with open('user.csv', 'w', newline='') as f:
  9. ### 表头传入
  10. writer = csv.DictWriter(f, headers)
  11. writer.writeheader()
  12. ### 一行一行写入
  13. for i in li:
  14. writer.writerow(i)
  15. ### 直接把li写入(多行)
  16. writer.writerows(li)
  17.  
  18. ### 查看结果
  19. [root@python _test]# python _test.py
  20. [root@python _test]# cat user.csv
  21. id,username,passwd
  22. 1,dream1,123
  23. 2,dream2,456
  24. 1,dream1,123
  25. 2,dream2,456

windows中查看:

  1. [root@python _test]# sz user.csv

总结 

到此这篇关于Python中CSV文件(逗号分割)的文章就介绍到这了,更多相关Python CSV文件逗号分割内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号