Change language

numpy.savetxt ()

| | | |

numpy.savetxt (fname, X, fmt = & # 39;%. 18e & # 39 ;, delimiter = & # 39; & # 39 ;, newline = & # 39; / n & # 39 ;, header = & # 39; & # 39 ;, footer = & # 39; & # 39 ;, comments = & # 39; # & # 39 ;, encoding = None) : this Method is used to save the array to a text file.

Parameters:
fname: If the filename ends in .gz , the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.
X : [1D or 2D array_like] Data to be saved to a text file.
fmt : A single format (% 10.5f), a sequence of formats, or a multi-format string, eg ’Iteration% d -% 10.5f’, in which case delimiter is ignored.
delimiter : String or character separating columns.
newline : String or character separating lines.
header : String that will be written at the beginning of the file.
footer : String that will be written at the end of the file.
comments : String that will be prepended to the header and footer strings, to mark them as comments. Default: ’#’, as expected by eg numpy.loadtxt.
encoding : Encoding used to encode the output file. Does not apply to output streams. If the encoding is something other than ’bytes’ or ’latin1’ you will not be able to load the file in NumPy versions & lt; 1.14. Default is ’latin1’.

Code # 1:

# Python program explaining
# savetxt () function

import numpy as geek

 

x = geek.arange ( 0 , 10 , 1 )

print ( " x is: " )

print (x)

  
# X is mac siv

c = geek.savetxt ( ’geekfile.txt’ , x, delimiter = ’,’

a = open ( "geekfile.txt" , ’r’ ) # open file in reading mode

  

print ( "the file contains:" )

print (a.read ())

Output:

 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 
Code # 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 3 arrays with the same size

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 ())

Output:

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 br /> 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 1.900000000000000000e + 01 1.900000000000000000e + 0100000000
0000000000 01 2.100000000000000000e + 01 2.200000000000 000000e + 01 2.300000000000000000e + 01 2.400000000000000000e + 01 2.500000000000000000e + 01 2.600000000000000000e + 01 2.700000000000000000e + 01 2.800000000000000000e + 01 2.900000000000000000e + 01

Code # 3: TypeError

# 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 - 3 arrays with the same size

c = geek.savetxt ( ’ geekfile.txt’ , (x, y, z)) 

Exit :

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’)

 hiccups measurement error.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically