一维数据
1.代码
import csv
list1=["name","age","school","address"]
filew=open('asheet.csv',"w")
filew.write(",".join(list1))
filew.close()
filew=open('asheet.csv','r')
line=filew.read()
print(line)
filew.close()
2.运行结果
二维数据
1.代码
import csv
datas=[["name","age","school","address"],
["李虎","23","北大","广州"],
["张三","56","清华","南京"],
["盛德","23","北大","北海"]]
filename='bsheet.csv'
with open(filename,'w',newline="") as f:
writer=csv.writer(f)
for row in datas:
writer.writerow(row)
ls=[]
with open(filename,'r') as f:
reader=csv.reader(f)
for row in reader:
print(reader.line_num,row)
ls.append(row)
print(ls)
2.运行结果
整齐读取
import csv
datas=[["name","age","school","address"],
["李虎","23","北大","广州"],
["张三","56","清华","南京"],
["盛德","23","北大","北海"]]
filename='bsheet.csv'
str1=''
with open(filename,'r') as f:
reader=csv.reader(f)
for row in reader:
for item in row:
str1+=item+'\t'
str1+='\n'
print(reader.line_num,row)
print(str1)
标签:库写,filew,Python,Excel,filename,reader,csv,open,row
From: https://blog.csdn.net/2401_82506219/article/details/145041017