python txt、excel讀寫
阿新 • • 發佈:2019-01-07
#從txt寫入excel
workbook = xlsxwriter.Workbook('植保網.xlsx') #建立工作簿
sheet = workbook.add_worksheet()
r=0
for line in open('zgzbao_link.1.txt','r',encoding='utf-8'):
line=eval(line)
for m in range(2):
sheet.write(r,m,line[m])
r=r+1
workbook.close()
#從excel寫入txt
xl = xlrd.open_workbook('植保網.xlsx')
table1 = xl.sheet_by_name(u"Sheet1")
nr=table1.nrows
f2= open('zgzbao_repeat.txt','w+',encoding='utf-8')
for i in range(nr):
row=table1.row_values(i)
f2.write(str(row[:2])+'\n')
f2.close()