- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 numpy.savetxt 周围使用了一个小包装器自动生成标题名称并为可读输出创建某种智能宽度对齐。更简单的解决方案位于 this answer .
我想知道如何指定宽度并使输出文本居中对齐,而不仅仅是文档中所示的左对齐。
从 numpy.savetxt
文档中,我看到以下信息:
Notes
-----
Further explanation of the `fmt` parameter
(``%[flag]width[.precision]specifier``):
flags:
``-`` : left justify
``+`` : Forces to preceed result with + or -.
``0`` : Left pad the number with zeros instead of space (see width).
width:
Minimum number of characters to be printed. The value is not truncated
if it has more characters.
文档指出 python mini format specification 上有更“详尽的资源” ,但那里的信息与对齐信息不兼容。
The meaning of the various alignment options is as follows:
Option Meaning
'<' Forces the field to be left-aligned within the available space (this is the default for most objects).
'>' Forces the field to be right-aligned within the available space (this is the default for numbers).
'=' Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form ‘+000000120’. This alignment option is only valid for numeric types.
'^' Forces the field to be centered within the available space.
不兼容是因为 savetxt 不接受 '^'
作为有效的格式字符。任何人都可以阐明如何在“numpy.savetxt”中指定格式以使输出居中对齐吗?
最佳答案
您可以使用 format
组合更复杂的格式选项,包括居中的 '^'
标志:
import numpy as np
a = np.ones((3,3))*100
a[1,1]=111.12321
a[2,2]=1
np.savetxt('tmp.txt',a, fmt='{:*^10}'.format('%f'))
给予:
****100.000000**** ****100.000000**** ****100.000000****
****100.000000**** ****111.123210**** ****100.000000****
****100.000000**** ****100.000000**** ****1.000000****
关于python - 如何居中对齐 numpy.savetxt 生成的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15555525/
这可能有一个令人尴尬的简单答案,但我不确定它是什么。 在我的Python代码中,有一部分我想将数组(称为“stokes_columns”,其中充满了 float )保存到文本文件中。 我通过以下方法做
我有以下代码: #Time integration T=28 AT=5/(1440) L=T/AT tr=np.linspace(AT,T,AT) %I set minimum_value to AT
使用 Numpy,我将拆分一个维度数组 (557124,2) , dtype = "S10" , 在 6 个子数组中使用: sub_arr = np.split(arr, 6) 现在我想使用 for循
如何使用 numpy.savetxt 在另一个目录中输出文本文件? 当前工作目录是/home/user/current/,但我想将txt文件保存到/home/user/test/。如何修改以下代码?谢
在使用 np.savetxt 时如何使用变量作为文件名 这是我的 MWE: import numpy as np k=0 x = [1, 2, 3] y = [4, 5, 6] zipped = zi
我有一个包含多个列的表格,我想用 保存它 numpy.savetxt('test.txt', test, fmt='%f') 我只想以浮点格式保存最后一列,所有其他列都应该是整数...例如 1 1 1
我正在尝试将一个数组写出到一个文本文件中,我希望每个元素都写成 int 类型。 我正在使用 np.savetxt(outfile_name, array, comments = '') 写出文件。我使
有这样的结构化数组: [ (b'H', 0.9425, 0.1412, 7.1414) ... (b'N', 1.0037, 4.0524, 6.8000)] 我想使用 numpy.savetxt 创
我正在尝试保存一个包含大量数字并包含标题的数据文件。我坚持将标题与相应的列对齐。示例代码: import numpy as np import os Output = np.empty((0,3),
我快完成这个程序了,但是我无法得到我想要的输出。这是代码: import numpy as np filename = (raw_input("Which file are we loading?
我正在尝试将矩阵另存为文本,该矩阵每行有 288 个 float ,末尾有 1 个字符串,我使用了这样的 savetxt: np.savetxt('name', matrix, delimiter='
我使用loadtxt来初始化数组。 source = np.loadtxt('source.txt').astype(int) 之后我在函数中使用这个数组,其主体是: file = open('joh
我正在尝试保存 5D 数组,但遇到此错误。 > Traceback (most recent call last): File "model_3.py", line 53, in > >
with file("features.txt", "w") as outfile: # temp_array is an array of shape (500L,) np.save
我有三个数组: a = np.array([1,2,3]) b = np.array([4,5,6]) c = np.array([7,8]) 我想做这样的事情: np.savetxt('data.t
我正在使用 numpy.savetxt() 将 numpy 数组写入 csv 文件,但生成的文件非常大。例如,如果我创建一个 zeros 数组: import numpy test = numpy.z
我有两个数组 A 和 B,这样: A.shape 是 (N, 1) 并且 dtype 是 int。B.shape为(N, M),dtype为float。 数组 A 包含 ids/序列号。信息。我想将
我正在尝试使用 numpy.savetxt 将一个 numpy 数组写入 .txt 文件.据我所知,以下代码遵循文档: z = np.array([1,2,3]) np.savetxt('testda
numpy savetxt 没有添加逗号分隔符 我有一个包含以下内容的数组: 3.880631596916139792e-01 6.835074831218364011e-01 4.604322858
我想将 numpy.savetxt 的结果加载到一个字符串中。本质上是以下没有中间文件的代码: import numpy as np def savetxts(arr): np.savetxt
我是一名优秀的程序员,十分优秀!