import csv
import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.randn(6,6), index=list(range(6)), columns=list(range(6)))
data.to_csv("test.csv",index=False,sep=',')# pd.DataFrame类型可写入csv
data_read = pd.read_csv('test.csv')
efile = open('test.csv','r')
csv_read = csv.reader(efile)
for line in csv_read:
print(line) # print读取line
with open('test.csv','r') as efile:
csv_read = csv.reader(efile)
for line in csv_read:
print(line) # print读取line
with open("test_write.csv","w") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["index","a_name","b_name"])#先写入columns_name
writer.writerows([[0,1,3],[1,2,3],[2,3,4]])#写入多行writerows