在 CSV 文件中存储和检索布尔值的便捷方法是什么

发布于 2024-09-19 03:50:30 字数 251 浏览 5 评论 0原文

如果我使用 CSV 模块存储布尔值,它会被 str() 函数转换为字符串 TrueFalse 。但是,当我加载这些值时,False 字符串的计算结果为 True,因为它是一个非空字符串。

我可以通过在读取时使用 IF 语句手动检查字符串来查看字符串是什么来解决这个问题,但这有点不太优雅。有更好的想法吗?或者这只是编程世界中的其中之一?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是暖光i 2024-09-26 03:50:45

我不确定回答你自己的问题是否是一种不好的形式,但这是我提出的解决方案。它基本上包括将我正在谈论的讨厌的 IF 语句分离到一个函数中。

def setyesNo(value):
    if value: return 'Yes'
    else: return 'No'

def checkYesNo(text):
    if text == 'Yes': return True
    else: return False

然后在我的 dictWriter 中执行此操作。

for item in mylist:
    writer.writerow( {'Is Cool' : setYesNo(item.is_cool),
                      .....
                      })

并在 dictReader 中。

for line in reader:
    item MyObject(is_Cool=checkYesNo(line['Is Cool']),
                  .....
                  )
    mylist.append(item)

I'm not sure if answering your own question is bad form or not, but here's the solution I've come up with. It basicaly consists of hiving off that pesky IF statement I was talking about into a function.

def setyesNo(value):
    if value: return 'Yes'
    else: return 'No'

def checkYesNo(text):
    if text == 'Yes': return True
    else: return False

Then in my dictWriter do this.

for item in mylist:
    writer.writerow( {'Is Cool' : setYesNo(item.is_cool),
                      .....
                      })

And in dictReader.

for line in reader:
    item MyObject(is_Cool=checkYesNo(line['Is Cool']),
                  .....
                  )
    mylist.append(item)
酒解孤独 2024-09-26 03:50:42

使用 int() 函数将布尔值转换为 int 值,然后存储它们。话虽这么说,伊莱-本德斯基的上述评论值得注意。

use the int() function to covert the boolean to their int values and then store those. that being said, eli-bendersky's comment above is worth noting.

≈。彩虹 2024-09-26 03:50:40

几乎总是有一种更简单(而且通常更方便)的方法,而且大多数时候它已经包含在 Python 令人惊叹的标准库中。

在这种情况下,您可以只使用 TrueFalse,然后使用 astliteral_eval 方法解析它们。

假设我们有一个名为 test.csv 的文件,其中包含以下内容:

nonetype,integer,boolean
None,123,False

我们还有一个 test.py 脚本来解析该文件:

import ast
import csv

with open('test.csv', 'r') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        nonetype = ast.literal_eval(row['nonetype'])
        integer = ast.literal_eval(row['integer'])
        boolean = ast.literal_eval(row['boolean'])
        print(type(nonetype), type(integer), type(boolean))

当我们运行该脚本时,我们可以看到这些值按预期进行解析。

$ python3 test.py
<class 'NoneType'> <class 'int'> <class 'bool'>

即使输入来自不受信任的来源,此方法也可以安全使用,更多信息 文档

There's almost always a simpler (and often more convenient) way, and most of the time it is already included in Python's amazing standard library.

In this case you can just use True and False, then parse them with ast's literal_eval method.

Let's say we have a file called test.csv with the following content:

nonetype,integer,boolean
None,123,False

We also have a test.py script to parse the file:

import ast
import csv

with open('test.csv', 'r') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        nonetype = ast.literal_eval(row['nonetype'])
        integer = ast.literal_eval(row['integer'])
        boolean = ast.literal_eval(row['boolean'])
        print(type(nonetype), type(integer), type(boolean))

When we run the script we can see that the values get parsed as expected.

$ python3 test.py
<class 'NoneType'> <class 'int'> <class 'bool'>

This method is also safe to use, even with input from untrusted sources, more info on the docs.

伪心 2024-09-26 03:50:38

在 CSV 文件中存储布尔值的方法

  • 字符串:两个常见的选择是truefalseTrueFalse,但我也见过
  • 整数:01
  • 浮点数:0.01.0

让我们比较一下各自的优点/缺点:

  • 字符串:
    • + 人类可以阅读
    • - CSV 读取器会将其作为字符串,并且当对其应用 bool 时,两者的计算结果都会为“true”
  • 整数:
    • + CSV 读者可能会看到此列是整数,并且 bool(0) 计算结果为 false。
    • + 更节省空间
    • - 不太清楚它是布尔值
  • 浮点数:
    • + CSV 读者可能会看到此列是整数,并且 bool(0.0) 计算结果为 false。
    • - 不太清楚它是布尔值
    • + 可能为 null(如 NaN)

Pandas CSV 阅读器显示所描述的行为。

将 Bool 字符串转换为 Bool 值

查看 mpu。 string.str2bool

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

其实现如下:

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_))

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_))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文