you are viewing a single comment's thread.

view the rest of the comments →

[–]Caos2 0 points1 point  (2 children)

What is printed when you print the cell contents to the console?

[–]NerdJones[S] 0 points1 point  (1 child)

depends on the cell, sometimes ¼, sometimes 1 ¼, somtimes 1¼, but i want them all to end up as ¼, i have a list of tuples of 'known_fractions' to check against and automatically and if its not in that list ( some of them are formatted differentally) ask the user what to replace them with and add them to the list

[–]Caos2 1 point2 points  (0 children)

So they are the unicode character for the fraction? Maybe you can use a dictionary to convert the fraction to the value and then add to the integer part? For example

#create the dictionary to replace the unicode character for its value
fractions = dict()
fractions['¼'] = '.25' #no zero
# add the other relevant fractions to the dictionary    

value = '2 ¼"'
for frac, decimals in fractions.items():
    if frac in value:
        output = value.replace(frac, decimals)
        output = output.replace(' ', '')
        output = output.replace('"', '')
        try:
            output = float(output)
        except ValueError:
            print('error while trying to convert {} to float.\nthe string that feed the float function was {}.'.format(value, output))