Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dígito verificador del RUT en Python
# encoding=utf-8
# Idea of a Custom data types for validate the Chilean RUT in Flask-RESTful
# by @lacosox
from itertools import cycle
#rut='XXXXXXXX-X'
def chilean_rut(rut_text):
rut_text.upper().strip().replace(".", "").replace("K", "10")
(rut, dv)=rut_text.split('-')
if rut.isdigit():
reversed_digits = map(int, reversed(str(rut)))
factors = cycle(range(2, 8))
s = sum(d * f for d, f in zip(reversed_digits, factors))
if dv <> str((-s) % 11):
raise ValueError("Invalid RUT, DV incorrect.")
else:
raise ValueError("Invalid RUT, only numbers is valid.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.