首页 > 代码库 > 读写csv文件

读写csv文件

import csvdata=[]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=0with 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+=1print(y)

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

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

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

读写csv文件