首页 > 编程语言 > 详细

Python将数据写入excel或者txt,读入csv格式或xls文件

时间:2018-08-12 15:11:44      阅读:302      评论:0      收藏:0      [点我收藏+]

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文件

原文:https://www.cnblogs.com/caiyishuai/p/9462833.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!