大家做在数据解决的时候,一定难免会与文件交互,那么对于指定的文件类型,我们如何操作呢?
1.python读写csv文件import csv #python2可以用file替代openwith open('test.csv','w') as csvFile: writer = csv.writer(csvFile) #先写columns_name writer.writerow(["index","a_name","b_name"]) #写入多行用writerows writer.writerows([[1,2,3],[0,1,2],[4,5,6]]) #用reder读取csv文件with open('test.csv','r') as csvFile: reader = csv.reader(csvFile) for line in reader: print line
肯定要注意,csv文件在写入时,字段和字段之间是用逗号“”,“”分割的,假如略微不注意,就会出现串行的情况。
2.python读写excel文件import xlwt#创立工作簿f = xlwt.Workbook() ''' 创立第一个sheet: sheet1 ''' #创立sheet sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) row0 = [u'编号',u'单位',u'网站备案号',u'域名',u'外链名称',u'不良网站名',u'不良url地址',u'地市',u'ip地址',u'截图',u'备注&