gpt4 book ai didi

python - 从文本文件中获取整数并用python写入excel

转载 作者:太空宇宙 更新时间:2023-11-04 05:56:34 24 4
gpt4 key购买 nike

我已经从文本文件中获取值并将其写入 excel 文件。但不知何故在 excel 单元格中用字符串编写的整数。所以单元格中有一个绿色三角形。像这样 This is the output of the file

我要这样打印

enter image description here

这是代码

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))

关于python - 从文本文件中获取整数并用python写入excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27457330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com