Python 根据原EXCEL表数据新增一列,如何实现?

Python 根据原EXCEL表数据新增一列,如何实现?,第1张

mport xlwtf = xlwt.Workbook() #创建工作簿sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheetl_=[1,2,3,4,5]for i in range(len(l_)):sheet1.write(0,i,i)#表格的第一行开始写。第一列,第二列。。。。 #sheet1.write(0,0,start_date,set_style('Times New Roman',220,True))f.save('text.xls')#保存文件

就是把插入行之后值重新输出来。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

import xlwt

import xlrd

from xlutils.copy import copy

#styleBoldRed = xlwt.easyxf('font: color-index red, bold on')

#headerStyle = styleBoldRed

#wb = xlwt.Workbook()

#ws = wb.add_sheet('sheetName')

#ws.write(0, 0, "Col1",headerStyle)

#ws.write(0, 1, "Col2", headerStyle)

#ws.write(0, 2, "Col3",headerStyle)

#wb.save('fileName.xls')

#open existed xls file

oldWb = xlrd.open_workbook("fileName.xls", formatting_info=True)

oldWbS = oldWb.sheet_by_index(0)

newWb = copy(oldWb)

newWs = newWb.get_sheet(0)

inserRowNo = 1

newWs.write(inserRowNo, 0, "value1")

newWs.write(inserRowNo, 1, "value2")

newWs.write(inserRowNo, 2, "value3")

for rowIndex in range(inserRowNo, oldWbS.nrows):

for colIndex in range(oldWbS.ncols):

newWs.write(rowIndex + 1, colIndex, oldWbS.cell(rowIndex, colIndex).value)

newWb.save('fileName.xls')

print "save with same name ok"

添加列名:

pd.read_csv(file, header=None, names = ['a','b','c'] )1

names为指定的名字

数据tab分隔的时候去读方式

sep指定

df = pd.read_csv('../input/data_train.csv',sep = '\t',encoding='GBK')


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11869871.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存