0

I try to send data from a French database to Netbox but spaces and accented characters trigger an error as in the example:

Post_dept = nb.dcim.sites.create([
     {
    "name":"Saône et Loire",
    "slug":"saône et loire",,
     }
)   
Pynetbox.RequestError :
[{"slug":["Enter a valid \"slug\" consisting of letters, numbers, underscores or hyphens."]}]

I have developed this function which goes through a dictionary list and converts each problematic character into a compatible format. But the function is heavy and the renaming of value is confusing. How to encode the fields to keep the original format and be compatible with the API?

Thanks!

def encode_netbox(list_of_dicts:list):
    """ Encodes the strings to be interpreted by the
    by the POST method of the Netbox API

    arg:
    dictionary list

    return:
    dictionary list

    replace accented characters with their non-accented equivalents
    replace all special characters and spaces with an underscore
    """
    # scans the list passed as argument to the function
    for dico in list_of_dicts:
        # scans the key, value of each item
        for k,v in dico.items():
            # if the value is of type string
            if type(v) is str
                # search for a match of special characters
                if re.findall('[êéèë]', v):
                    # assigns to the key, the value with the modified character
                    dico[k]=re.sub('[êéèë]',"e",v)

        for k,v in dico.items():
            if type(v) is str
                if re.findall('[àâä@]',v):
                    dico[k]=re.sub('[àâä@]',"a",v)

        for k,v in dico.items():
            if type(v) is str: 
                if re.findall('[ùûü]',v):
                    dico[k]=re.sub('[ùûü]',"u",v)

        for k,v in dico.items():
            if type(v) is str: 
                if re.findall('[ôö]',v):
                    dico[k]=re.sub('[ôö]',"o",v)

        for k,v in dico.items():
            if type(v) is str:
                if re.findall('[œ]',v):
                    dico[k]=re.sub('[œ]',"oe",v)

        for k,v in dico.items():
            if type(v) is str:
                if re.findall('[ç]',v):
                    dico[k]=re.sub('[ç]',"c",v)

        for k,v in dico.items():
            if type(v) is str:
                if re.findall('[îï]',v):
                    dico[k]=re.sub('[îï]',"i",v)

        for k,v in dico.items():
            if type(v) is str:
                if re.findall('[\W+]',v):
                    dico[k]=re.sub('[\W+]','_',v)
    # [\w+] ou [a-zA-Z0-9_-] Matches any letter, digit, underscore or hyphen
    return list_of_dicts
  • 1
    "How to encode the fields to keep the original format and be compatible with the API?" You can’t because the "original format" implies accents and the API doesn’t accept them. You could use a library like [`python-slugify`](https://pypi.org/project/python-slugify/) instead of implementing your own function. – bfontaine Apr 25 '23 at 08:26
  • Thank you for your help – Airhmine Jun 22 '23 at 19:28

0 Answers0