我有一个来自 datetime 的日期对象的 numpy 数组,我必须将其转换为字符串,以便我可以将它用作 SQL 语句的一部分(对于 mysql)。一般来说,这工作得很好:
dats = str(dates_list)
dats = string.replace(dats[1:-1],' ',"','")
除非它足够长,printdates_list
显示...(例如:printnumpy.arange(4000)
)。
我尝试直接使用 repr 特殊方法(是的,我知道我不应该),它应该提供可用于重建对象的字符串表示形式。显然,我无法使用 [ 0 1 2 ..., 4997 4998 4999]
构建此数组。
如何获得整个数组的字符串表示形式?
最佳答案
转换为列表似乎有效:
dats = str(list(dates_list))
但更正确的选择是更改 numpy 的 printoptions 。有一个 threshold
选项,用于设置打印摘要而不是完整报告的限制。默认值为 1000。您可以将其更改为无穷大:
import numpy as np
np.set_printoptions(threshold=np.inf)
dats = str(dates_list)
关于string - numpy 大数组到字符串获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13561868/