Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a convenient way to store and retrieve boolean values in a CSV file

Tags:

python

csv

If I store a boolean value using the CSV module, it gets converted to the strings True or False by the str() function. However, when I load those values, a string of False evaluates to being True because it's a non-empty string.

I can work around it by manually checking the string at read time with an IF statement to see what the string is, but it's somewhat less than elegant. Any better ideas, or is this just one of those things in the programming world?

like image 930
Simon Hibbs Avatar asked Sep 15 '10 12:09

Simon Hibbs


1 Answers

Ways to store boolean values in CSV files

  • Strings: Two common choices aretrue and false, True and False, but I've also seen yes and no.
  • Integers: 0 or 1
  • Floats: 0.0 or 1.0

Let's compare the respective advantages / disadvantages:

  • Strings:
    • + A human can read it
    • - CSV readers will have it as a string and both will evaluate to "true" when bool is applied to it
  • Integers:
    • + CSV readers might see that this column is integer and bool(0) evaluates to false.
    • + A bit more space efficient
    • - Not totally clear that it is boolean
  • Floats:
    • + CSV readers might see that this column is integer and bool(0.0) evaluates to false.
    • - Not totally clear that it is boolean
    • + Possible to have null (as NaN)

The Pandas CSV reader shows the described behaviour.

Convert Bool strings to Bool values

Have a look at mpu.string.str2bool:

>>> str2bool('True')
True
>>> str2bool('1')
True
>>> str2bool('0')
False

which has the following implementation:

def str2bool(string_, default='raise'):
    """
    Convert a string to a bool.

    Parameters
    ----------
    string_ : str
    default : {'raise', False}
        Default behaviour if none of the "true" strings is detected.

    Returns
    -------
    boolean : bool

    Examples
    --------
    >>> str2bool('True')
    True
    >>> str2bool('1')
    True
    >>> str2bool('0')
    False
    """
    true = ['true', 't', '1', 'y', 'yes', 'enabled', 'enable', 'on']
    false = ['false', 'f', '0', 'n', 'no', 'disabled', 'disable', 'off']
    if string_.lower() in true:
        return True
    elif string_.lower() in false or (not default):
        return False
    else:
        raise ValueError('The value \'{}\' cannot be mapped to boolean.'
                         .format(string_))
like image 194
Martin Thoma Avatar answered Sep 18 '22 12:09

Martin Thoma