Search code examples
pythonparsingfloating-pointintegerstring-concatenation

Change Floating Point Integer to Integer in Output


I am trying to write a Python script to parse Excel data and produce a custom output.

The part where I am stuck is that the output adds ".0" onto the end of any fields from the spreadsheet that contained a number. The string concatenation that I'm doing is treating these numeric values as floating point integers.

How can I make sure that the output is a regular integer rather than a floating point integer for any numeric values?

This is my script so far:

import xlrd

book = xlrd.open_workbook('/Users/doctorwho/test.xls')
sheet = book.sheet_by_index(0)

myList = []
for i in range(sheet.nrows):
   myList.append(sheet.row_values(i))

outFile = open('/Users/doctorwho/update.txt', 'wb')

for i in myList:
   outFile.write("Test data 1" + str(i[1]) + "Test data 2" + str(i[2])

Solution

  • Try using

    for i in myList:
           outFile.write("Test data 1" + str(int(i[1])) + "Test data 2" + str(int(i[2]))