python3 xlwt,csv学习

前言

对于抓取一些站点分析然后指纹识别的时候可能用到到它。所以学习下。这里就记录一些最基本的感觉有用的。

xlwt

基本创建

demo:

#coding=utf-8
import xlwt
yunying = xlwt.Workbook(encoding = 'utf-8') 
sheet1 = yunying.add_sheet('sheet1') #创建一个sheet
sheet1.write(0,0, 'yunying_0')       #添加数据,(0,0,xxx)=>(行,列,内容)
yunying.save('yunying.xls')          #保存为.xls

设置宽度

#coding=utf-8
import xlwt
yunying = xlwt.Workbook(encoding = 'utf-8')
sheet1 = yunying.add_sheet('sheet1',cell_overwrite_ok=True)
sheet1.write(0,0, 'yunying_0000')
sheet1.col(0).width = 3333 #第一排宽度
yunying.save('yunying.xls')

添加公式

#coding=utf-8
import xlwt
yunying = xlwt.Workbook(encoding = 'utf-8')
sheet1 = yunying.add_sheet('sheet1')
sheet1.write(0,0, 4)
sheet1.write(0,1, 5)
sheet1.write(1,0,xlwt.Formula('A1*B1'))
sheet1.write(1,1,xlwt.Formula('A1+B1'))
sheet1.col(0).width = 3333
yunying.save('yunying.xls')

 

追加写入

由于xlwt库只能新建写入,不能追加写入。当我们有时候并不是一次性输入,是间断性输入。

xlutils 库的 copy 功能可能帮助我们打破这个局限性

#coding=utf-8
import xlwt
import xlrd
import xlutils.copy

rd = xlrd.open_workbook("yunying.xls", formatting_info = True)   # 打开文件,formatting_info 保存原有的样式
wt = xlutils.copy.copy(rd)   # 复制
sheets = wt.get_sheet(0)   # 读取第一个工作
sheets.write(2, 0, 2)   # 向 m-1 行 n-1 列的单元格写入内容
sheets.write(2, 1, 'xxx公司后台弱口令登录')
wt.save("yunyinga.xls")   # 保存b

原:

 

现:

学习链接:

https://www.cnblogs.com/machangwei-8/p/10738244.html

https://blog.csdn.net/qq_38161040/article/details/88525927

csv

这里就学习了一下DictWriter的写入方式,以字典的写入方式

demo:

import csv
data0={'Hostname':'www.cxx.com','IP':'0.0.0.0',"Title":"五金工业"}
data1={'Hostname':'955.cxx.com','IP':'0.0.0.0',"Title":"六金工业"}
data=[data0,data1]
with open('out.csv', 'w', newline='',encoding='utf-8-sig') as csvfile:
        fieldnames = ['Hostname', 'IP','Title']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()
        for i in range(len(data)):
            writer.writerow(data[i])

posted @ 2020-04-03 20:52  yunying  阅读(779)  评论(0编辑  收藏  举报