I believe that this version deals with all the cases but I would not be shocked if there were problems...
#!/usr/bin/env python
# coding: utf-8
from __future__ import (absolute_import, division, print_function, unicode_literals)
def fractions_to_floats(s):
frac_part = ''
output = []
for x in reversed(s.split()): # processing terms from right to left
a, _, b = x.partition('/')
if b:
frac_part = '{}{}'.format(int(a) / int(b.strip('\'"')), b[-1])
else:
if x[-1] in '\'"':
output += [x, frac_part]
else:
output.append(x + frac_part.lstrip('0'))
frac_part = ''
output.append(frac_part)
return ' '.join(output)
def feet_and_inches(s='''5.5' 6.5" 3.6' 8.6"'''):
if '/' in s:
s = fractions_to_floats(s)
assert '/' not in s, 'fractions_to_floats() failed: ' + s
inches = sum(float(x.replace("'", '')) * 12 if "'" in x
else float(x.replace('"', '')) for x in s.split())
return '{:g}\' {:g}"'.format(inches // 12, inches % 12)
if __name__ == '__main__':
test_data = '''
0
1
6"
13
14"
5'
4' 1' "4
4' 5" 1'
5' 6"
5' 6" 1"
5' 6" 3' 8"
5' 6.5" 3' 8.5"
5.5' 6" 3.5' 8"
5.5' 6.5" 3.5' 8.5"
5.5' 6.5" 3.5' 8.6"
5.5' 6.5" 3.6' 8.6"
15 1/64"
15 1/32"
15 1/16"
15 1/8"
15 1/4"
15 1/2"
0' 1" 2' 3" 4' 5" 6 7/8"
15 1/16'
15 1/8'
15 1/4'
15 1/2'
16' 2 1/16"
16' 2 1/8"
16' 2 1/4"
16' 2 1/2"
16 1/2' 2 1/16"
16 1/2' 2 1/8"
16 1/2' 2 1/4"
16 1/2' 2 1/2"
16' 1' 2 1/16"
16' 1' 2 1/8"
16' 1' 2 1/4"
16' 1' 2 1/2"
16.5' 1' 2 1/16"
16.5' 1' 2 1/8"
16.5' 1' 2 1/4"
16.5' 1' 2 1/2"'''.splitlines()
print('=' * 20)
for line in test_data:
print('{:>24} ==> {}'.format(line.strip(), feet_and_inches(line)))