Python读写CSV

简单记录下,关于这两种文件的读写:

读写csv:

import csv
#读取数据
def getCsv(filename="xxxxxxxx"):
    rows=[]
    with open(filename,'rb') as f:
        readers=csv.reader(f,delimiter=',',quotechar='|')
        next(readers,None)
        for row in readers:
            rows.append(row)
        print rows
#写入数据
def  writeCsv(filename="xxxxxxxxx"):
    with open(filename,'wb') as f:
        write =csv.writer(f)
        write.writerow("Element","system")
        data={
            ('selenium','webdriver python'),
            ('appnium','android python'),
            ('appnium','ios')
            }
        write.writerows(data)
        f.close()
if __name__=='__main__':
    writeCsv()
    getCsv()

  关于with的用法,可以参考别人的解释:

http://blog.csdn.net/suwei19870312/article/details/23258495/
关于readers=csv.reader(f,delimiter=',',quotechar='|')这句的解释可以参考别人的解释:
http://blog.csdn.net/kaka_yidian/article/details/13000235
读取到某一列:
 1 def getCsv(value1,value2,filename="xxxxxxxx"):
 2     rows=[]
 3     with open(filename,'rb') as f:
 4         readers=csv.reader(f,delimiter=',',quotechar='|')
 5         next(readers,None)
 6         for row in readers:
 7             rows.append(row)
 8         return rows(value1,value2)
 9 if __name__=='__main__':
10     print getCsv(0,0)
posted @ 2017-09-12 18:01  绿叶1208  阅读(718)  评论(0编辑  收藏  举报