Reading 1 2 3 4 5 From File and Storing Ins String Array

eleven. Reading and Writing Information Files: ndarrays

Past Bernd Klein. Last modified: 01 Feb 2022.

In that location are lots of ways for reading from file and writing to data files in numpy. We volition discuss the unlike ways and corresponding functions in this chapter:

  • savetxt
  • loadtxt
  • tofile
  • fromfile
  • save
  • load
  • genfromtxt

Saving textfiles with savetxt

Scrabble with the Text Numpy, read, write, array

The first two functions nosotros will cover are savetxt and loadtxt.

In the following simple example, we ascertain an assortment x and save it as a textfile with savetxt:

            import            numpy            as            np            10            =            np            .            array            ([[            1            ,            two            ,            iii            ],            [            4            ,            5            ,            6            ],            [            7            ,            8            ,            9            ]],            np            .            int32            )            np            .            savetxt            (            "test.txt"            ,            x            )          

The file "test.txt" is a textfile and its content looks similar this:

          [email protected]:~/Dropbox/notebooks/numpy$ more than test.txt 1.000000000000000000e+00 2.000000000000000000e+00 three.000000000000000000e+00 4.000000000000000000e+00 5.000000000000000000e+00 vi.000000000000000000e+00 vii.000000000000000000e+00 8.000000000000000000e+00 ix.000000000000000000e+00        

Attending: The above output has been created on the Linux command prompt!

It's likewise possible to print the assortment in a special format, similar for instance with three decimal places or as integers, which are preceded with leading blanks, if the number of digits is less than 4 digits. For this purpose we assign a format string to the tertiary parameter 'fmt'. We saw in our first example that the default delimeter is a bare. We can change this behaviour past assigning a string to the parameter "delimiter". In most cases this string will consist solely of a single graphic symbol only information technology tin can be a sequence of character, like a smiley " :-) " besides:

            np            .            savetxt            (            "test2.txt"            ,            x            ,            fmt            =            "            %two.3f            "            ,            delimiter            =            ","            )            np            .            savetxt            (            "test3.txt"            ,            x            ,            fmt            =            "            %04d            "            ,            delimiter            =            " :-) "            )          

The newly created files await like this:

          [email protected]:~/Dropbox/notebooks/numpy$ more test2.txt  i.000,ii.000,3.000 4.000,5.000,6.000 7.000,8.000,9.000          [email protected]:~/Dropbox/notebooks/numpy$ more test3.txt  0001 :-) 0002 :-) 0003 0004 :-) 0005 :-) 0006 0007 :-) 0008 :-) 0009        

The complete syntax of savetxt looks similar this:

savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\northward', header='', footer='', comments='# ')        
Parameter Meaning
Ten array_like Data to be saved to a text file.
fmt str or sequence of strs, optional
A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. 'Iteration %d -- %10.5f', in which case 'delimiter' is ignored. For complex '10', the legal options for 'fmt' are:
a) a unmarried specifier, "fmt='%.4e'", resulting in numbers formatted like "' (%south+%sj)' % (fmt, fmt)"
b) a full cord specifying every existent and imaginary part, e.g. "' %.4e %+.4j %.4e %+.4j %.4e %+.4j'" for three columns
c) a list of specifiers, ane per cavalcade - in this case, the real and imaginary part must take separate specifiers, e.g. "['%.3e + %.3ej', '(%.15e%+.15ej)']" for two columns
delimiter A string used for separating the columns.
newline A string (e.m. "\n", "\r\n" or ",\n") which will cease a line instead of the default line catastrophe
header A String that will be written at the beginning of the file.
footer A String that volition be written at the end of the file.
comments A String that will exist prepended to the 'header' and 'footer' strings, to mark them equally comments. The hash tag '#' is used as the default.

Loading Textfiles with loadtxt

Nosotros volition read in now the file "exam.txt", which we take written in our previous subchapter:

              y              =              np              .              loadtxt              (              "examination.txt"              )              print              (              y              )            

OUTPUT:

[[ 1.  2.  3.]  [ four.  5.  6.]  [ 7.  8.  9.]]            
              y              =              np              .              loadtxt              (              "test2.txt"              ,              delimiter              =              ","              )              print              (              y              )            

OUTPUT:

[[ 1.  two.  3.]  [ four.  v.  6.]  [ 7.  viii.  9.]]            

Nothing new, if nosotros read in our text, in which we used a smiley to separator:

              y              =              np              .              loadtxt              (              "test3.txt"              ,              delimiter              =              " :-) "              )              print              (              y              )            

OUTPUT:

[[ one.  2.  3.]  [ four.  v.  half-dozen.]  [ 7.  8.  9.]]            

It'due south also possible to cull the columns by index:

              y              =              np              .              loadtxt              (              "test3.txt"              ,              delimiter              =              " :-) "              ,              usecols              =              (              0              ,              2              ))              print              (              y              )            

OUTPUT:

[[ 1.  3.]  [ 4.  6.]  [ seven.  nine.]]            

We will read in our adjacent example the file "times_and_temperatures.txt", which we accept created in our affiliate on Generators of our Python tutorial. Every line contains a time in the format "hh::mm::ss" and random temperatures between ten.0 and 25.0 degrees. Nosotros have to convert the time cord into float numbers. The time will be in minutes with seconds in the hundred. We ascertain first a function which converts "hh::mm::ss" into minutes:

              def              time2float_minutes              (              time              ):              if              type              (              time              )              ==              bytes              :              fourth dimension              =              time              .              decode              ()              t              =              time              .              split              (              ":"              )              minutes              =              float              (              t              [              0              ])              *              lx              +              float              (              t              [              ane              ])              +              float              (              t              [              2              ])              *              0.05              /              iii              return              minutes              for              t              in              [              "06:00:ten"              ,              "06:27:45"              ,              "12:59:59"              ]:              print              (              time2float_minutes              (              t              ))            

OUTPUT:

360.1666666666667 387.75 779.9833333333333            

You might have noticed that we bank check the type of time for binary. The reason for this is the employ of our function "time2float_minutes in loadtxt in the post-obit instance. The keyword parameter converters contains a dictionary which tin can concur a part for a column (the key of the cavalcade corresponds to the key of the dictionary) to convert the string information of this cavalcade into a float. The cord data is a byte cord. That is why nosotros had to transfer it into a a unicode cord in our function:

              y              =              np              .              loadtxt              (              "times_and_temperatures.txt"              ,              converters              =              {              0              :              time2float_minutes              })              print              (              y              )            

OUTPUT:

[[  360.     20.1]  [  361.5    16.1]  [  363.     sixteen.ix]  ...,   [ 1375.5    22.5]  [ 1377.     11.1]  [ 1378.v    15.2]]            
            # delimiter = ";" , # i.east. use ";" as delimiter instead of whitespace                      

tofile

tofile is a function to write the content of an array to a file both in binary, which is the default, and text format.

A.tofile(fid, sep="", format="%due south")

The data of the A ndarry is always written in 'C' order, regardless of the gild of A.

The information file written by this method can be reloaded with the part fromfile().

Parameter Meaning
fid can be either an open file object, or a string containing a filename.
sep The string 'sep' defines the separator between assortment items for text output. If it is empty (''), a binary file is written, equivalent to file.write(a.tostring()).
format Format string for text file output. Each entry in the array is formatted to text by commencement converting it to the closest Python type, and so using 'format' % particular.

Remark:

Information on endianness and precision is lost. Therefore it may not be a good idea to utilize the function to archive information or ship data betwixt machines with unlike endianness. Some of these problems can be overcome by outputting the information as text files, at the expense of speed and file size.

              dt              =              np              .              dtype              ([(              'fourth dimension'              ,              [(              'min'              ,              int              ),              (              'sec'              ,              int              )]),              (              'temp'              ,              float              )])              ten              =              np              .              zeros              ((              1              ,),              dtype              =              dt              )              x              [              'time'              ][              'min'              ]              =              10              x              [              'temp'              ]              =              98.25              print              (              x              )              fh              =              open up              (              "test6.txt"              ,              "bw"              )              ten              .              tofile              (              fh              )            

OUTPUT:

Live Python preparation

instructor-led training course

Upcoming online Courses

Enrol here

fromfile

fromfile to read in data, which has been written with the tofile function. It'due south possible to read binary data, if the data type is known. It'southward as well possible to parse simply formatted text files. The data from the file is turned into an assortment.

The full general syntax looks like this:

numpy.fromfile(file, dtype=float, count=-one, sep='')

Parameter Meaning
file 'file' can be either a file object or the proper noun of the file to read.
dtype defines the data type of the array, which will be synthetic from the file information. For binary files, it is used to determine the size and byte-order of the items in the file.
count defines the number of items, which will exist read. -1 ways all items will be read.
sep The string 'sep' defines the separator between the items, if the file is a text file. If information technology is empty (''), the file will exist treated as a binary file. A space (" ") in a separator matches zero or more whitespace characters. A separator consisting solely of spaces has to match at to the lowest degree one whitespace.
              fh              =              open up              (              "test4.txt"              ,              "rb"              )              np              .              fromfile              (              fh              ,              dtype              =              dt              )            

OUTPUT:

array([((4294967296, 12884901890), i.0609978957e-313),        ((30064771078, 38654705672), 2.33419537056e-313),        ((55834574860, 64424509454), three.60739284543e-313),        ((81604378642, 90194313236), 4.8805903203e-313),        ((107374182424, 115964117018), half-dozen.1537877952e-313),        ((133143986206, 141733920800), 7.42698527006e-313),        ((158913789988, 167503724582), 8.70018274493e-313),        ((184683593770, 193273528364), 9.9733802198e-313)],        dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])
              import              numpy              as              np              import              os              # platform dependent: difference between Linux and Windows              #data = np.arange(50, dtype=np.int)              data              =              np              .              arange              (              l              ,              dtype              =              np              .              int32              )              data              .              tofile              (              "test4.txt"              )              fh              =              open              (              "test4.txt"              ,              "rb"              )              # 4 * 32 = 128              fh              .              seek              (              128              ,              os              .              SEEK_SET              )              x              =              np              .              fromfile              (              fh              ,              dtype              =              np              .              int32              )              print              (              ten              )            

OUTPUT:

[32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]            

Attention:

It can cause problems to utilise tofile and fromfile for data storage, because the binary files generated are not platform contained. There is no byte-society or data-type information saved by tofile. Data can be stored in the platform independent .npy format using save and load instead.

Best Practise to Load and Save Data

The recommended way to store and load data with Numpy in Python consists in using load and relieve. Nosotros as well use a temporary file in the following :

              import              numpy              equally              np              print              (              x              )              from              tempfile              import              TemporaryFile              outfile              =              TemporaryFile              ()              ten              =              np              .              arange              (              x              )              np              .              save              (              outfile              ,              x              )              outfile              .              seek              (              0              )              # Only needed hither to simulate endmost & reopening file              np              .              load              (              outfile              )            

OUTPUT:

[32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49] array([0, 1, 2, three, 4, 5, 6, 7, eight, 9])

and yet another way: genfromtxt

There is all the same another manner to read tabular input from file to create arrays. Every bit the name implies, the input file is supposed to be a text file. The text file can exist in the form of an archive file as well. genfromtxt can procedure the archive formats gzip and bzip2. The type of the archive is adamant by the extension of the file, i.eastward. '.gz' for gzip and bz2' for an bzip2.

genfromtxt is slower than loadtxt, but it is capable of coping with missing data. Information technology processes the file data in two passes. At outset it converts the lines of the file into strings. Thereupon it converts the strings into the requested information type. loadtxt on the other hand works in ane go, which is the reason, why it is faster.

recfromcsv(fname, **kwargs)

This is not actually another way to read in csv data. 'recfromcsv' basically a shortcut for

np.genfromtxt(filename, delimiter=",", dtype=None)

Live Python training

instructor-led training course

Upcoming online Courses

Enrol here

harrisgothys.blogspot.com

Source: https://python-course.eu/numerical-programming/reading-and-writing-data-files-ndarrays.php

0 Response to "Reading 1 2 3 4 5 From File and Storing Ins String Array"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel