numpy.savetxt()

numpy.savetxt()

numpy.savetxt(fname, X, fmt='%.18e', delimiter='', newline='/n', header='', footer='', comments='#', encoding=None) 这个方法用来保存一个数组到一个文本文件。

参数:
fname : 如果文件名以.gz结尾,该文件会自动保存为压缩的gzip格式。 loadtxt能透明地理解gzipped文件。
X :[一维或二维数组_like] 要保存到文本文件的数据。
fmt: 一个单一的格式(%10.5f),一个格式序列,或一个多格式字符串,例如’迭代%d – %10.5f’,在这种情况下,分隔符被忽略。
delimiter: 分隔列的字符串或字符。
newline: 分隔行的字符串或字符。
header: 将被写在文件开头的字符串。
footer: 将被写在文件末尾的字符串。
comments: 将被预置到页眉和页脚字符串中的字符串,以标记它们为注释。默认:’#’,如numpy.loadtxt所期望的那样。
encoding: 用来对输出文件进行编码的编码。不适用于输出流。如果编码不是’bytes’或’latin1’,你将无法在NumPy版本< 1.14中加载文件。默认是’latin1’。

代码 #1:

# Python program explaining  
# savetxt() function
import numpy as geek
  
x = geek.arange(0, 10, 1)
print("x is:")
print(x)
  
# X is an array
c = geek.savetxt('geekfile.txt', x, delimiter =', ')   
a = open("geekfile.txt", 'r')# open file in read mode
  
print("the file contains:")
print(a.read())

输出 :

x is:
[0 1 2 3 4 5 6 7 8 9]
the file contains:
0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
4.000000000000000000e+00
5.000000000000000000e+00
6.000000000000000000e+00
7.000000000000000000e+00
8.000000000000000000e+00
9.000000000000000000e+00


代码 #2:

# Python program explaining  
# savetxt() function
  
import numpy as geek
  
x = geek.arange(0, 10, 1)
y = geek.arange(10, 20, 1)
z = geek.arange(20, 30, 1)
print("x is:")
print(x)
  
print("y is:")
print(y)
  
print("z is:")
print(z)
  
# x, y, z are 3 numpy arrays with same dimension 
c = geek.savetxt('geekfile.txt', (x, y, z)) 
a = open("geekfile.txt", 'r')# open file in read mode
  
print("the file contains:")
print(a.read())

输出 :

x is:   
[0 1 2 3 4 5 6 7 8 9]   
y is:   
[10 11 12 13 14 15 16 17 18 19]   
z is:   
[20 21 22 23 24 25 26 27 28 29] 

the file contains:   
0.000000000000000000e+00 1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00 7.000000000000000000e+00 8.000000000000000000e+00 9.000000000000000000e+00   
1.000000000000000000e+01 1.100000000000000000e+01 1.200000000000000000e+01 1.300000000000000000e+01 1.400000000000000000e+01 1.500000000000000000e+01 1.600000000000000000e+01 1.700000000000000000e+01 1.800000000000000000e+01 1.900000000000000000e+01   
2.000000000000000000e+01 2.100000000000000000e+01 2.200000000000000000e+01 2.300000000000000000e+01 2.400000000000000000e+01 2.500000000000000000e+01 2.600000000000000000e+01 2.700000000000000000e+01 2.800000000000000000e+01 2.900000000000000000e+01 

代码#3:类型错误

# Python program explaining  
# savetxt() function
  
import numpy as geek
  
x = geek.arange(0, 10, 1)
y = geek.arange(0, 20, 1)
z = geek.arange(0, 30, 1)
print("x is:")
print(x)
  
print("y is:")
print(y)
  
print("z is:")
print(z)
  
# x, y, z are 3 numpy arrays without having same dimension 
c = geek.savetxt('geekfile.txt', (x, y, z)) 

输出:

fh.write(asbytes(format % tuple(row) + newline))   
TypeError: only length-1 arrays can be converted to Python scalars 

During handling of the above exception, another exception occurred: 

% (str(X.dtype), format))   
TypeError: Mismatch between array dtype (‘object’) and format specifier (‘%.18e’) 

注意,如果numpy数组的维度不相等,就会发生错误。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程