zoukankan      html  css  js  c++  java
  • python处理csv文档

    在工作中遇到了使用python解析csv文件的问题,包括读写操作,下面参考官网文档,进行一下总结:

    首先CSV (Comma Separated Values) ,也就是逗号分开的数值,可以用Notepad,写字板,excel等打开,如下图:

    在python的官网说明文档中提到,python中的csv模块对于Unicode编码不支持,不过我们一般也都用来存储UTF-8 or printable ASCII 的数值吧。

    对于csv文件的读取操作的例子如下所示:

     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 import csv
     4 
     5 
     6 #add a new line in the end
     7 csvfile = file('youtube_boundingboxes_detection_train.csv', 'a+') #read in csv file
     8 writer = csv.writer(csvfile)
     9 writer.writerow(['last','100'])
    10 csvfile.close()
    11 
    12 csvfile = file('youtube_boundingboxes_detection_train.csv', 'rb') #read in csv file
    13 reader = csv.reader(csvfile)
    14 for line in reader:
    15     print(line)

    上面包含了读写两种操作。

    另外还可以对分隔符进行操作,也就是说不一定非得是逗号分隔的,具体的参加官网吧。

  • 相关阅读:
    Java面试知识点总结
    Log4j常用配置及使用
    Eclipse+tomcat+axis2进行web service部署
    iframe内点击a标签禁止滚动到顶部
    VScode首选项
    Bookmarks
    VScode常用插件
    slideout
    bs-loading
    iPhone X 上删除白条
  • 原文地址:https://www.cnblogs.com/rainsoul/p/6385629.html
Copyright © 2011-2022 走看看