2

I already get the value from text file and write it to excel file. But somehow in the excel cell the integer written in string. So there's a green triangle in the cell. Like this This is the output of the file

I want to print like this

enter image description here

and this is the code

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"
4
  • You can type cast value in the statement. That is sheet.write(rowx, colx, float(value)). That has always worked for me. Do try and check. Commented Dec 13, 2014 at 9:33
  • csv won't implicitly cast the numbers to float; you have to do it manually - sheet.write(rowx, colx, float(value)). Commented Dec 13, 2014 at 9:33
  • write() in python always takes an argument of type str. Therefore, it will always write string in the file. Commented Dec 13, 2014 at 9:36
  • 1
    @AmitSharma this isn't file.write, it's xlwt.Worksheet.write. Commented Dec 13, 2014 at 9:42

1 Answer 1

1

From the 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.

Thus a str input will be taken as a string in excel. Thus you will have to type cast it explicitly.

In your program this can be done from changing a single line:

from

 sheet.write(rowx, colx, value)

to

 sheet.write(rowx, colx, float(value))
Sign up to request clarification or add additional context in comments.

3 Comments

sheet.write(rowx, colx, float(value)) ValueError: could not convert string to float: Filename: its still not working. Is there other way?
yeah there have other strings in it.. So I'll try to split it first. Thanks
i try it with no string file but it still error TypeError: float() argument must be a string or a number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.