Pandas to_csv with quoting=3 (QUOTE_NONNUMERIC) 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28541302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:57:14  来源:igfitidea点击:

Pandas to_csv with quoting=3 (QUOTE_NONNUMERIC) doesn't work

pythonpandas

提问by jozxyqk

From the docsregarding to_csv()and others:

从关于和其他人的文档中to_csv()

quoting : int, Controls whether quotes should be recognized. Values are taken from csv.QUOTE_* values. Acceptable values are 0, 1, 2, and 3 for QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONE, and QUOTE_NONNUMERIC, respectively.

quoting : int,控制是否应该识别引号。值取自 csv.QUOTE_* 值。对于 QUOTE_MINIMAL、QUOTE_ALL、QUOTE_NONE 和 QUOTE_NONNUMERIC,可接受的值分别为 0、1、2 和 3。

Setting quoting=3still does not quote strings even if they're not numeric, and libreofficeis constantly defaulting to splitting by spaces which I never realise until its too late. How can I write CSV, quoting strings with spaces correctly?

quoting=3即使字符串不是数字,设置仍然不引用字符串,并且libreoffice不断默认按空格分割,我直到为时已晚才意识到。如何编写 CSV,正确引用带空格的字符串?

回答by jozxyqk

It seems like the value in the csvlibraryhas changed since these docs were written. Rather than use the magic number 3, use csv.QUOTE_NONNUMERICto be safe...

自从编写这些文档以来,csv库中的值似乎发生了变化。而不是使用幻数3,使用csv.QUOTE_NONNUMERIC是安全的......

>>> import csv
>>> csv.QUOTE_NONNUMERIC
2

In full:

在全:

table.to_csv("myfile.csv", quoting=csv.QUOTE_NONNUMERIC)