Home > Software engineering >  How to validate character belongs to the portable character set
How to validate character belongs to the portable character set

Time:11-28

Number of POSIX standard refer to the Portable Character Set.

I need to check that user input corresponds to the standards and consists only from the acceptable characters. Is there any convenient way to do the check?

There is tedious approach to manually port table from the wikipedia:

portable_set = '\0\a\b...'
def check(sample):
     return all(c in portable_set for c in sample)

But POSIX is all round us, so I believe somewhere in the python standard library such set should be already defined. But I don't know the location to find it.

CodePudding user response:

I don't believe such a set exists built-in in python. If it did exist I'd expect it to reside in the string module, and it's not there.

However, python does have string.printable, which I'm pretty sure contains all but the first three elements of the portable character set. You can make your definition more terse by just tacking the remainder onto it:

import string

portable_set = set(string.printable   '\0\a\b')
def check(sample):
    return set(sample).issubset(portable_set)

CodePudding user response:

string from the Python standard library includes some string constants. One of those is string.printable. I think that is what you are looking for.

import string
string.printable

You can read more about string and other constants here: https://docs.python.org/3/library/string.html .

  • Related