Я спробував це. Це трохи виходить за межі питання: Ви отримуєте введення. Спочатку він буде перетворений у рядок (якщо це список, наприклад з Гарний суп); то до int, то до плавання.
Це заходить так далеко, наскільки це можливо. У гіршому випадку він повертає все неперетворене як рядок.
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'це все одно дастьValueError!).