python - 如何将 numpy 字符串数组(带逗号)保存到 CSV?

标签 python arrays postgresql csv numpy

tl;dr 答案:不要使用 numpy。使用 csv.writer 而不是 numpy.savetxt

我是 Python 和 NumPy 的新手。将二维字符串数组(包含逗号)保存到 CSV 文件似乎并不难,但我无法按我想要的方式工作。

假设我有一个看起来像这样的数组(由列表的列表组成):

[['text1, text2', 'text3'],
['text4', 'text5']]

我想要一个在 Excel 中看起来像这样(或没有引号字符)的 CSV 文件(竖线 = 单元格分隔符):

'text1, text2' | 'text3'
'text4'        | 'text5'

我正在使用 numpy.savetxt(filename, array, fmt="%s"),我得到以下 CSV 输出(带方括号):

['text1, text2','text3']
['text4','text5']

在 Excel 中显示如下:

['text1  | text2' | 'text3']
['text4' | 'text5']

我尝试使用 savetxt 定界符参数,但输出没有变化。

我需要手动执行此操作吗?如果是这样,请告诉我是否有任何我应该注意的捷径。

最后,我需要将 CSV 文件导入 Postgresql 数据库。我不完全清楚 CSV 格式究竟需要什么才能按预期工作,但我假设如果它在 Excel 中看起来不对,它可能最终会在 Postgres 中搞砸。 Postgres documentation说:

The values in each record are separated by the DELIMITER character. If the value contains the delimiter character, the QUOTE character, the NULL string, a carriage return, or line feed character, then the whole value is prefixed and suffixed by the QUOTE character, and any occurrence within the value of a QUOTE character or the ESCAPE character is preceded by the escape character. You can also use FORCE_QUOTE to force quotes when outputting non-NULL values in specific columns.

谢谢!

++++++++++++++++++++++++++++++

真实的输入和输出,如果它有相关的不同:

数组:

[['8908232', 'Plant Growth Chamber Facility at the Department of Botany, University of Wisconsin-Madison', 'DBI', 'INSTRUMENTAT & INSTRUMENT DEVP', '1/1/90', '12/19/89', 'WI', 'Standard Grant', 'Joann P. Roskoski', '12/31/91', '$94,914.00 ', 'BIO', '1108', '', '$0.00 ']]

CSV 输出:

['8908232', 'Plant Growth Chamber Facility at the Department of Botany, University of Wisconsin-Madison', 'DBI', 'INSTRUMENTAT & INSTRUMENT DEVP', '1/1/90', '12/19/89', 'WI', 'Standard Grant', 'Joann P. Roskoski', '12/31/91', '$94,914.00 ', 'BIO', '1108', '', '$0.00 ']

Excel 版本:

['8908232'   'Plant Growth Chamber Facility at the Department of Botany  University of Wisconsin-Madison'    'DBI'   'INSTRUMENTAT & INSTRUMENT DEVP'    '1/1/90'    '12/19/89'  'WI'    'Standard Grant'    'Joann P. Roskoski'     '12/31/91'  '$94   914.00 '     'BIO'   '1108'  ''  '$0.00 ']                  

最佳答案

添加 fmt="%s" 不会在每个字段周围加上引号——引号是字符串 %s 的 Python 字符串文字的一部分,并且 %s 只是说任何值都应该格式化为字符串。如果您想强制在所有内容周围加上引号,您需要在格式字符串中使用引号,例如 fmt='"%s"'

但是,即使您不这样做,您显示的行也可能不会产生您显示的输出。 NumPy 无法将您的逗号更改为管道字符,或使用管道字符作为分隔符。您唯一可以获得的是通过添加 delimiter=' |'。如果你添加它......它没有任何变化,你会得到这个:

text1, text2 | text3
text4 | text5

因此,无论您的实际问题是什么,都不可能是您描述的问题。


与此同时,如果您尝试尽可能灵活地为非数字数据编写 CSV 文件,标准库的 csv模块比 NumPy 强大得多。顾名思义,NumPy 的优势在于处理数字 数据。以下是使用 csv 执行此操作的方法:

with open(filename, 'wb') as f:
    csv.writer(f).writerows(array)

这将默认为 , 作为分隔符。由于您的某些字符串中包含 , 字符,默认情况下,它会引用这些字符串。但是您可以配置引用/转义行为、引号字符、定界符以及 NumPy 无法配置的各种其他内容。

关于python - 如何将 numpy 字符串数组(带逗号)保存到 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20624428/

相关文章:

python - 文字冒险室运动

python - 如何在 Airflow 中通过 SSH 并运行 PythonOperator

python - 无法在 Python 3.5 DLL 加载失败时导入 cv2

ios - 使用按钮快速循环遍历数组项

sql - 梦幻足球数据postgre sql获取一审

python - 用于多个起始值和终止值的矢量化 NumPy linspace

python - IndexError : too many indices. 1 行 2 列的 Numpy 数组

javascript - Array.find() 或 Array.some() 但返回自定义值

postgresql - Elixir : check if postgresql map column has key

postgresql - 匿名函数postgresql的返回值