numpy.fromstring用任何白点隔开

2024-06-16 10:24:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文档¹,其中n维矩阵存储为:

1 2 3 4
5 6 7 8

9 10 11 12
13 14 15 16

精确的维度存储在元数据中。4和5用\n隔开,而不是. However, numpy.fromstring('1 2\n3 4', sep=" ")做了我想做的事情,尽管文档中有其他建议。我对依赖与定义的行为不同的实现感到犹豫。在

为什么numpy.fromstring("1 2\n3 4", sep=" ")和{}给了我(想要的)array([ 1., 2., 3., 4.]),还有没有一个更健壮的(如:对应于文档化/定义的行为)具有相同的效果?在


¹文档是一个XML文件,树中最低元素的文本表示数字数据。我无法控制此文件格式。


Tags: 文件数据文档numpy定义矩阵xml事情
1条回答
网友
1楼 · 发布于 2024-06-16 10:24:34

如果你得到的是你想要的(意思是array([ 1., 2., 3., 4.])是你想要的行为),那么我相信这在{a1}的文档中得到了解释,这是参数sep-

sep : str, optional

If not provided or, equivalently, the empty string, the data will be interpreted as binary data; otherwise, as ASCII text with decimal numbers. Also in this latter case, this argument is interpreted as the string separating numbers in the data; extra whitespace between elements is also ignored.

(重点是我的)

在这种情况下,\n被认为是额外的空白,因此被忽略。请注意忽略并不意味着前后元素成为一个单一的元素,它们仍然是独立的数字。一个类似的例子,\t。在

In [16]: np.fromstring('1\t2\t3\t4', sep=" ")
Out[16]: array([ 1.,  2.,  3.,  4.])

相关问题 更多 >