python csv之writerow()和writerows()的区别
import csvname=[(“src_ip”,“dst_ip”,“src_port”,“dst_port”,“ul_pkts”,“dl_pkts”,“ul_flag”,“dl_flag”)]with open(“test2.csv”,“a”) as result:writer=csv.writer(result)writer.writerow(name)writer.writerows(na
import csv
name=[(“src_ip”,“dst_ip”,“src_port”,“dst_port”,“ul_pkts”,“dl_pkts”,“ul_flag”,“dl_flag”)]
with open(“test2.csv”,“a”) as result:
writer=csv.writer(result)
writer.writerow(name)
writer.writerows(name)
writerow():单行写入,将一个列表全部写入csv的同一行
writerows():多行写入,将一个二维列表的每一个列表写为一行
例子:
name=[(“src_ip”,“dst_ip”,“src_port”,“dst_port”,“ul_pkts”,“dl_pkts”,“ul_flag”,“dl_flag”)]
使用writer.writerow(name),得到结果:
“(‘src_ip’, ‘dst_ip’, ‘src_port’, ‘dst_port’, ‘ul_pkts’, ‘dl_pkts’, ‘ul_flag’, ‘dl_flag’)”(元组被当做一个整体)
使用writer.writerows(name),得到结果:src_ip,dst_ip,src_port,dst_port,ul_pkts,dl_pkts,ul_flag,dl_flag
name=(“src_ip”,“dst_ip”,“src_port”,“dst_port”,“ul_pkts”,“dl_pkts”,“ul_flag”,“dl_flag”)
使用writer.writerow(name),得到结果:
src_ip,dst_ip,src_port,dst_port,ul_pkts,dl_pkts,ul_flag,dl_flag
使用writer.writerows(name),得到结果
s,r,c,_,i,p
d,s,t,_,i,p
s,r,c,_,p,o,r,t
d,s,t,_,p,o,r,t
u,l,_,p,k,t,s
d,l,_,p,k,t,s
u,l,_,f,l,a,g
d,l,_,f,l,a,g
更多推荐
所有评论(0)