从文本文件获取整数,写入Excel与Python

我已经从文本文件中获取值并将其写入excel文件。 但不知何故在excel单元格中写入的整数string。 所以这个单元格里有一个绿色的三angular形 喜欢这个 这是文件的输出

我想打印这样的

在这里输入图像说明

这是代码

from itertools import chain import glob ,csv, sys, os sys.path.insert(0,'D:/apera/Python27/xlwt-0.7.5') import xlwt openMesureFile = 'D:/apera/Workspace/Python scripting test 2/sounding0.txt' savePlace = 'D:/apera/Workspace/Sounding/sounding{0:03d}.txt' openSoundingFile = 'D:/apera/Workspace/Sounding/*.txt' with open(openMesureFile, 'rb') as inf: header = next(inf) for index, line in enumerate(inf,start=0): with open(savePlace.format(index) ,'w') as outf: outf.write('Filename:;%s\n\n' %outf.name) outf.write(header) for line in chain([line], inf): if 'Sounding :;Sondage n°' in line: header = line break outf.write(line) for filename in glob.glob(openSoundingFile): wb = xlwt.Workbook(encoding="latin1") sheet = wb.add_sheet('INPUT') newName = filename spamReader = csv.reader(open(filename, 'rb'), delimiter=';',quotechar='"') for rowx, row in enumerate(spamReader): for colx, value in enumerate(row): sheet.write(rowx, colx, value) sheet.col(0).width = 5555 sheet.col(1).width = 11110 sheet.col(2).width = 5555 sheet.col(3).width = 3333 wb.save(newName[:-4] + ".xls") print "success" 

从文档

写(r,c,label =“”,style = Style.default_style)[#]
标签要写入的数据值。 int,long或decimal.Decimal实例转换为float。

因此在excel中strinput将被视为一个string 。 因此你必须input明确的types。

在你的程序中,这可以通过改变一行来完成:

  sheet.write(rowx, colx, value) 

  sheet.write(rowx, colx, float(value))