作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 numpy.savetxt 编写一个 numpy 数组,可以选择写入标准输出,或作为文件的一部分(通过重定向标准输出)。例如(在Python3中)
import numpy as np
import sys
def output( a ):
print( 'before np.savetxt' )
np.savetxt( sys.stdout.buffer, a )
print( 'after np.savetxt' )
a = np.array( [ ( 1, 2, 3 ), ( 4, 5, 6 ) ] )
output( a )
with open( 'test', 'w' ) as file_out:
sys.stdout = file_out
output( a )
这会写入(到标准输出):
before np.savetxt
1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00
4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00
after np.savetxt
但是“测试”文件包含这些乱序的条目:
> cat test
1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00
4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00
before np.savetxt
after np.savetxt
是否可以使这些输出保持一致的顺序,或者我应该使用不同的方法将 numpy.savetxt
与其他命令结合起来写入同一文件?
最佳答案
您只能在 output()
函数中使用 savetxt
命令,传递 header
和 footer
论据:
def output( a ):
header = 'before np.savetxt'
footer = 'after np.savetxt'
np.savetxt(sys.stdout.buffer, a, header=header, footer=footer)
正如 @DavidParks 所提醒的那样,对于 Python 2.7,它应该是 sys.stdout
,对于 Python 3.x,它应该是 sys.stdout.buffer
。
关于python - 重定向 sys.stdout 时 numpy savetxt 乱序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23041516/
序 大家好呀,我是summo,这次来写写我在上班空闲(摸鱼)的时候做的一个小网站的事。去年阿里云不是推出了个活动嘛,2核2G的云服务器一年只要99块钱,懂行的人应该知道这个价格在业界已经是非常良心了
我尝试根据给定的级别顺序(BFS 顺序)构造 BST。我知道这是可能的,但我不知道我该怎么写。问题是我必须使用 BFS 序列。所以,我不能在这里使用递归,我必须迭代地编写我的程序......我发现这有
我是一名优秀的程序员,十分优秀!