在 CSV 文件中存储和检索布尔值的便捷方法是什么
如果我使用 CSV 模块存储布尔值,它会被 str()
函数转换为字符串 True
或 False
。但是,当我加载这些值时,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定回答你自己的问题是否是一种不好的形式,但这是我提出的解决方案。它基本上包括将我正在谈论的讨厌的 IF 语句分离到一个函数中。
然后在我的 dictWriter 中执行此操作。
并在 dictReader 中。
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.
Then in my dictWriter do this.
And in dictReader.
使用 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.
几乎总是有一种更简单(而且通常更方便)的方法,而且大多数时候它已经包含在 Python 令人惊叹的标准库中。
在这种情况下,您可以只使用
True
和False
,然后使用ast
的literal_eval
方法解析它们。假设我们有一个名为
test.csv
的文件,其中包含以下内容:我们还有一个
test.py
脚本来解析该文件:当我们运行该脚本时,我们可以看到这些值按预期进行解析。
即使输入来自不受信任的来源,此方法也可以安全使用,更多信息 文档。
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
andFalse
, then parse them withast
'sliteral_eval
method.Let's say we have a file called
test.csv
with the following content:We also have a
test.py
script to parse the file:When we run the script we can see that the values get parsed as expected.
This method is also safe to use, even with input from untrusted sources, more info on the docs.
在 CSV 文件中存储布尔值的方法
true
和false
、True
和False
,但我也见过是
和否
。0
或1
0.0
或1.0
让我们比较一下各自的优点/缺点:
+
人类可以阅读-
CSV 读取器会将其作为字符串,并且当对其应用bool
时,两者的计算结果都会为“true”+
CSV 读者可能会看到此列是整数,并且bool(0)
计算结果为 false。+
更节省空间-
不太清楚它是布尔值+
CSV 读者可能会看到此列是整数,并且bool(0.0)
计算结果为 false。-
不太清楚它是布尔值+
可能为 null(如 NaN)Pandas CSV 阅读器显示所描述的行为。
将 Bool 字符串转换为 Bool 值
查看
mpu。 string.str2bool
:其实现如下:
Ways to store boolean values in CSV files
true
andfalse
,True
andFalse
, but I've also seenyes
andno
.0
or1
0.0
or1.0
Let's compare the respective advantages / disadvantages:
+
A human can read it-
CSV readers will have it as a string and both will evaluate to "true" whenbool
is applied to it+
CSV readers might see that this column is integer andbool(0)
evaluates to false.+
A bit more space efficient-
Not totally clear that it is boolean+
CSV readers might see that this column is integer andbool(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
:which has the following implementation: