码迷,mamicode.com
首页 > 其他好文 > 详细

读写csv文件

时间:2016-09-20 10:05:20      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

import csv
data=[]
column1=[]
with open("C:\\Users\\cai\\Desktop\\homeland.csv", "r") as csvfile1:
    reader=csv.reader(csvfile1)
    for i in reader:
        #print(i[2])
        column1.append(i[2])
    #column1 = [i[2] for i in reader][1:]
    print(zheshi1,column1)
y=0
with open("C:\\Users\\cai\\Desktop\\International.csv", "r") as csvfile:
    read = csv.reader(csvfile)
    for row1 in read:
        print(row1)
        #print(row1[3])
        print(len(column1))
        for j in range(len(column1)):
            #print(column1[j])
            if row1[3] == column1[j]:
                print(column1[j])
                y+=1
                print(type(row1))
                with open(C:\\Users\\cai\\Desktop\\changge.csv,wb) as row3:
                    witer=csv.writer(row3)
                    print(type(row1))
                    witer.writerow(row1)
                    print(finsh)
               # writer=csv.writer(open(‘C:\\Users\\cai\\Desktop\\changge.csv‘,‘wb‘))
                break


            else:
                j+=1
print(y)

进行对文件进行读取并且写入, 读取的时候一定需要

with open("C:\\Users\\cai\\Desktop\\homeland.csv", "r") as csvfile1:

这样子的格式,才可以进行。否则就会报错。

读写csv文件

标签:

原文地址:http://www.cnblogs.com/caicaihong/p/5887427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!