保存numpy的阵列到txt文件作为一个单独的列? [英] Saving numpy array to txt file as a single column?

查看:855
本文介绍了保存numpy的阵列到txt文件作为一个单独的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个numpy的阵列到txt文件:

I'm trying to write a numpy array into txt file:

a = numpy.array([1,2,3])
numpy.savetxt('a.txt',a,fmt='%.3f')

当我打开txt文件,它看起来像:

when I open the txt file it looks like:

1.0002.0003.000

但是当我在Word中粘贴它看起来像:

but when I paste it in word it looks like:

1.000

2.000

3.000

的问题是,另一个程序读取txt文件由线输入线

The problem is that another program reads the txt file as input line by line:

data = fid.readlines()

因此​​,它不工作correctly.How我能解决这个问题?

As a result it doesn't work correctly.How can I fix this problem?

推荐答案

numpy.savetxt 有一个关键字参数换行默认为 \\ n (在UNIX / Linux的换行符)。

numpy.savetxt has a keyword argument newline which defaults to \n (the unix/linux line break).

您可以手动设置或使用 os.linesep 来选择你的当前操作系统的换行符。因此,

You can either set it manually or use os.linesep to choose the newline character of your current operating system. So

import os
import numpy as np

a = np.array([1,2,3])
np.savetxt('a.txt', a, fmt='%.3f', newline=os.linesep)  

应该是与Windows编辑和在Windows下运行应该能够阅读它的节目一列。

should be in one column with a windows editor and a program which runs under windows should be able to read it.

这篇关于保存numpy的阵列到txt文件作为一个单独的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆