我已经从文本文件中获取值并将其写入 excel 文件。但不知何故在 excel 单元格中用字符串编写的整数。所以单元格中有一个绿色三角形。像这样
我要这样打印
这是代码
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"
请您参考如下方法:
来自docs
write(r, c, label="", style=Style.default_style) [#]
label The data value to be written. An int, long, or decimal.Decimal instance is converted to float.
因此 str 输入在 excel 中将被视为 string。因此,您必须明确地对其进行类型转换。
在您的程序中,这可以通过更改一行来完成:
来自
sheet.write(rowx, colx, value)
到
sheet.write(rowx, colx, float(value))
