Próbowałem tego. To wykracza nieco poza pytanie: otrzymujesz wkład. Najpierw zostanie przekonwertowany na ciąg znaków (jeśli jest to lista, na przykład z Beautiful soup); potem do int, a potem do float.
To idzie tak daleko, jak to tylko możliwe. W najgorszym przypadku zwraca wszystko nieprzekonwertowane jako ciąg.
def to_normal(soupCell):
''' converts a html cell from beautiful soup to text, then to int, then to float: as far as it gets.
US thousands separators are taken into account.
needs import locale'''
locale.setlocale( locale.LC_ALL, 'english_USA' )
output = unicode(soupCell.findAll(text=True)[0].string)
try:
return locale.atoi(output)
except ValueError:
try: return locale.atof(output)
except ValueError:
return output
'C'nadal dałoby toValueError!).