Python將資料寫入excel或者txt,讀入csv格式或xls檔案

來源:互聯網
上載者:User

標籤: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檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.