numpy savetxt没有添加逗号分隔符

时间:2017-02-06 12:44:22

标签: python numpy

numpy savetxt未添加逗号分隔符

我有一个包含以下内容的数组:

3.880631596916139792e-01
6.835074831218364011e-01
4.604322858429276133e-01
3.494236368132551673e-01
7.142120448019100287e-01
2.579415438181463793e-01
8.230159985476581674e-01
7.342531681855216652e-01
3.196536650498674748e-01
7.444435819161493439e-01

我保存如下:

 np.savetxt('x.train.1.txt',XTraining, delimiter=',') 

但是当我查看txt文件时,没有逗号。

3 个答案:

答案 0 :(得分:7)

我想默认用例是存储列表列表,这就是为什么你需要将它作为一个列表的列表来处理:

np.savetxt('x.train.1.txt',[XTraining], delimiter=',')

或者使用逗号而不是换行符(注意:这会添加一个尾随逗号)

np.savetxt('x.train.1.txt',XTraining, newline=',')

答案 1 :(得分:2)

如果您希望它们分开并以逗号分隔

QPainterPath p = item->path();
for(int i = 0; i < p.elementCount(); i++)
{
    QPointF pt = p.elementAt(i); //get the value of the point at index i
    p.setElementPositionAt(i, 3, 1416); //change the value of the point at index i
}

答案 2 :(得分:0)

您可以指定格式,例如fmt='%d,',在输出中根据需要使用逗号。

np.savetxt('x.train.1.txt',XTraining, fmt='%d,') 

也适用于多列

np.savetxt('x.train.1.txt',XTraining, fmt='%s, %f, %f,')