標籤:src xlrd div app color tin xls read 需要
1.寫入excel,一開始不需要自己建立一個excel,會自動產生
attribute_proba是我寫入的對象
import xlwt myexcel = xlwt.Workbook() sheet = myexcel.add_sheet(‘sheet‘) si=-1 sj=-1 for i in attribute_proba: si=si+1 for j in i: sj=sj+1 sheet.write(si,sj,str(j)) sj=-1 myexcel.save("attribute_proba_big.xls")
2.寫入txt,一開始就需要你建立一個txt檔案
f=open(‘F:/goverment/myfinalcode/predict.txt‘, ‘w‘) for i in range(s): f.write(str(predict[i])) f.write(‘\n‘) f.write("寫好了") f.close()
3.讀入csv
file = ‘F:/goverment/myfinalcode/test_big.csv‘ fo=open(file) ls=[] for line in fo: line=line.replace("\t",",") line=line.replace("\n",",") line=line.replace("\"",",") ls.append(line.split(",")) for i in ls: li=[] for j in i: if j == ‘‘: continue li.append(str(j)) testdata.append(li)
from pandas import read_csv data_set = read_csv("F:/goverment/excel operating/type_in.csv") data = data_set.values[:, :] test_data = [] for line in data: ls = [] for j in line: ls.append(j) test_data.append(ls)
4.讀入xls
import xlrd file = ‘F:/goverment/myfinalcode/test_big_label.xls‘ wb = xlrd.open_workbook(file) ws = wb.sheet_by_name("Sheet1") for r in range(ws.nrows): col = [] for c in range(ws.ncols): col.append(ws.cell(r, c).value) testlabel.append(col)
Python將資料寫入excel或者txt,讀入csv格式或xls檔案