Найти - Пользователи
Полная версия: Переменные в re.sub
Начало » Python для новичков » Переменные в re.sub
1
vanvanov
Нужно заменить “US1234567” на “US 1,234,567”
Делаю
print(re.sub("US([0-9])([0-9][0-9][0-9])([0-9][0-9][0-9])","US \1\2\3","In US1234567 we can notice..."))
Получаю
In US [здесь идут квадраты] we can notice...
Как исправить?
sergeek
re.sub('\d+',lambda m: ' {:,}'.format(int(m.group(0))) ,"In US1234567 we can notice...")
Out[57]: 'In US 1,234,567 we can notice...'
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB