python 读取和写入csv文件

时间:2022-01-26 19:57:46

python csv读取和写入csv文件的简单应用

#写入
import csv
dic = {'./a.c':1, './b.c':2, './d.c':3}
csvFile = open('csvFile.csv','w', newline='')
writer = csv.writer(csvFile)
for key in dic:
writer.writerow([key, dic[key]])

writer.writerow(['./a.c',2,7])
writer.writerow(['./c.c',2,8])

csvFile.close()

#读取
csvFile = open("csvFile.csv", "r")
reader = csv.reader(csvFile) # 返回的是迭代类型
data = []
for line in reader:
print(line)
data.append(line)

print(int(data[0][1])*4)

csvFile.close()