<filename>std_tools.py
# -*- coding: utf-8 -*-
"""
Created on Thu Oct  3 11:23:22 2019

@author: cidm
"""
import re
import unicodedata

def strip_accents(text):
    """
    Compatible with python 2.x and 3.x
    Strip accents from input string.

    :param text: The input string.
    :type text: string.

    :returns: processed string.
    :rtype: string.
    """
    try:
        text = unicode(text, 'utf-8')
    except (TypeError, NameError): # unicode is a default on python 3 
        pass
    text = unicodedata.normalize('NFD', text)
    text = text.encode('ascii', 'ignore')
    text = text.decode("utf-8")
    return str(text)

def text_to_id(text, num=True):
    """
   Convert input text to id.

    :param text: The input string.
    :param num: if True removes the numbers. If false leave inside the name numbers and removes trailing character.
     num=False is ideal to remove trailing numbers only.
    :type text: String.

    :returns: The processed String.
    :rtype: String.
    """
    text = strip_accents(str(text).lower())
    text = re.sub('[ ]+', '_', text)
    if num:
        text = re.sub('[^0-9a-zA-Z_-]', '', text)
    else:
        text = re.sub('[^a-zA-Z_-]', '', text)
        text = text[:-1]
    return text

def insert_cnpj(num):
    
    """
    Cast a string of digits to the formatted 00.000.000/0001-00 CNPJ standard.
    """
    cnpj = num[:2]+'.'+num[2:5]+'.'+num[5:8]+r'/'+num[8:12]+'-'+num[12:]
    return cnpj

def make_cnpj(series):
    
    """
    Force the CNPJ 00.000.000/0001-00 standard on a pandas series object.
    """
    series = series.apply(lambda x: str(x))
    series = series.apply(lambda x: x.replace('.',''))
    series = series.apply(lambda x: x.replace('-',''))
    series = series.apply(lambda x: x.replace('/',''))
    series = series.apply(lambda x: x.zfill(14))
    series = series.apply(lambda x: insert_cnpj(x))
    
    return series

def insert_cpf(a):
    
    """
    Cast a string of digits to the formatted 000.000.000-00 CPF standard.
    """
    cpf = a[:3]+'.'+a[3:6]+'.'+a[6:9]+'-'+a[9:]
    return cpf

def make_cpf(series):
    
    """
    Force the CPF 000.000.000-00 standard on a pandas series object.
    """
    series = series.apply(lambda x: str(x))
    series = series.apply(lambda x: x.replace('.',''))
    series = series.apply(lambda x: x.replace('-',''))
    series = series.apply(lambda x: x.replace('/',''))
    series = series.apply(lambda x: x.zfill(11))
    series = series.apply(lambda x: insert_cpf(x))
    
    return series

def make_money(value):
    
    """
    Replaces comma with dot in order to treat the values as currency data.
    """
    value =str(value)
    value = re.sub('[,]', '.', value)
    value = float(value)
    return value

def make_currency(series):
    
    """
    Applies make_money on a pandas series object.
    """
    series = series.apply(lambda x: make_money(x))
    return series
<filename>std_tools.py
# -*- coding: utf-8 -*-
"""
Created on Thu Oct  3 11:23:22 2019

@author: cidm
"""
import re
import unicodedata

def strip_accents(text):
    """
    Compatible with python 2.x and 3.x
    Strip accents from input string.

    :param text: The input string.
    :type text: string.

    :returns: processed string.
    :rtype: string.
    """
    try:
        text = unicode(text, 'utf-8')
    except (TypeError, NameError): # unicode is a default on python 3 
        pass
    text = unicodedata.normalize('NFD', text)
    text = text.encode('ascii', 'ignore')
    text = text.decode("utf-8")
    return str(text)

def text_to_id(text, num=True):
    """
   Convert input text to id.

    :param text: The input string.
    :param num: if True removes the numbers. If false leave inside the name numbers and removes trailing character.
     num=False is ideal to remove trailing numbers only.
    :type text: String.

    :returns: The processed String.
    :rtype: String.
    """
    text = strip_accents(str(text).lower())
    text = re.sub('[ ]+', '_', text)
    if num:
        text = re.sub('[^0-9a-zA-Z_-]', '', text)
    else:
        text = re.sub('[^a-zA-Z_-]', '', text)
        text = text[:-1]
    return text

def insert_cnpj(num):
    
    """
    Cast a string of digits to the formatted 00.000.000/0001-00 CNPJ standard.
    """
    cnpj = num[:2]+'.'+num[2:5]+'.'+num[5:8]+r'/'+num[8:12]+'-'+num[12:]
    return cnpj

def make_cnpj(series):
    
    """
    Force the CNPJ 00.000.000/0001-00 standard on a pandas series object.
    """
    series = series.apply(lambda x: str(x))
    series = series.apply(lambda x: x.replace('.',''))
    series = series.apply(lambda x: x.replace('-',''))
    series = series.apply(lambda x: x.replace('/',''))
    series = series.apply(lambda x: x.zfill(14))
    series = series.apply(lambda x: insert_cnpj(x))
    
    return series

def insert_cpf(a):
    
    """
    Cast a string of digits to the formatted 000.000.000-00 CPF standard.
    """
    cpf = a[:3]+'.'+a[3:6]+'.'+a[6:9]+'-'+a[9:]
    return cpf

def make_cpf(series):
    
    """
    Force the CPF 000.000.000-00 standard on a pandas series object.
    """
    series = series.apply(lambda x: str(x))
    series = series.apply(lambda x: x.replace('.',''))
    series = series.apply(lambda x: x.replace('-',''))
    series = series.apply(lambda x: x.replace('/',''))
    series = series.apply(lambda x: x.zfill(11))
    series = series.apply(lambda x: insert_cpf(x))
    
    return series

def make_money(value):
    
    """
    Replaces comma with dot in order to treat the values as currency data.
    """
    value =str(value)
    value = re.sub('[,]', '.', value)
    value = float(value)
    return value

def make_currency(series):
    
    """
    Applies make_money on a pandas series object.
    """
    series = series.apply(lambda x: make_money(x))
    return series
# -*- coding: utf-8 -*- Created on Thu Oct  3 11:23:22 2019
 
 @author: cidm Compatible with python 2.x and 3.x
     Strip accents from input string.
 
     :param text: The input string.
     :type text: string.
 
     :returns: processed string.
     :rtype: string. # unicode is a default on python 3 Convert input text to id.
 
     :param text: The input string.
     :param num: if True removes the numbers. If false leave inside the name numbers and removes trailing character.
      num=False is ideal to remove trailing numbers only.
     :type text: String.
 
     :returns: The processed String.
     :rtype: String. Cast a string of digits to the formatted 00.000.000/0001-00 CNPJ standard. Force the CNPJ 00.000.000/0001-00 standard on a pandas series object. Cast a string of digits to the formatted 000.000.000-00 CPF standard. Force the CPF 000.000.000-00 standard on a pandas series object. Replaces comma with dot in order to treat the values as currency data. Applies make_money on a pandas series object.