0

I have a text file which i load in python, i load the file and split the values.The text file contains the following...

   678John Jones          23501289100005670
   999Arthur Dudley-Smith 40000956064012300
  5789Bob Billards        04600230190048945
543780Helen W Anderson    53001945045000990

for example i create an array called Hours[] then append the number to it by...

Hours.append(int(line[26:30]))

doing this give the value for Hours[0] to be 2350, but i want there to be a decimal place, so it should be 23.50

I cant figure out how to do this and any help i could get would be great

1
  • 1
    Could you just divide by 100? Commented Oct 1, 2015 at 3:45

2 Answers 2

1

Just use a floating point number:

Hours.append(float(line[26:30]) / 100.0)

Or if you're going to be dealing with large numbers, you might want to check out the decimal module.

Sign up to request clarification or add additional context in comments.

Comments

1

Take the first bit, and a dot, and the second bit:

>>> line = '   678John Jones          23501289100005670'
>>> line[26:28] + '.' + line[28:30]
'23.50'

Convert it to numeric one way:

>>> from decimal import Decimal
>>> Decimal('23.50')
Decimal('23.50')

If you must convert it to float, the one-trailing-zero doesn't really exist - 23.5 == 23.50, it's only there if you format it as a string and ask for a trailing zero.

>>> int(line[26:28]) + 0.01* int(line[28:30])
23.5

>>> '{0:0.2f}'.format(23.5)
'23.50'

Comments

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.