python3文本txt转Excal
执行脚本
import xlwt
import codecs #编码模块
n=1
m=1
f=codecs.open("./user.txt","rb","gbk","ignore") #编码为gbk
g=codecs.open("./pass2.txt","rb","gbk","ignore") #编码为gbk
a=f.readlines() #阅读文本,字符串
b=g.readlines()
book = xlwt.Workbook(encoding='utf-8') # 创建workbook对象,相当于创建一个Excel文件
sheet = book.add_sheet('output',cell_overwrite_ok=True) # 创建一个sheet对象,一个sheet对象对应Excel文件中的一张表格。其中的Output是这张表的名字,cell_overwrite_ok,表示是否可以覆盖单元格,其实是Worksheet实例化的一个参数,默认值是False
sheet.write(0, 0, 'username') # 插入数据
sheet.write(0, 1, 'password')
for i in a:
sheet.write(m,0,i)
m+=1
for j in b:
sheet.write(n,1,j)
n+=1
book.save('output.xls') #将以上操作保存到指定的Excel文件中