How To Read Data From A File And Save It As An Array
How to read a numerical data or file in Python with numpy?
Prerequisites: Numpy
NumPy is a full general-purpose assortment-processing package. Information technology provides a high-operation multidimensional array object and tools for working with these arrays. This article depicts how numeric data can be read from a file using Numpy.
Numerical data can be present in different formats of file :
- The data can be saved in a txt file where each line has a new data betoken.
- The information tin can be stored in a CSV(comma separated values) file.
- The data tin can be likewise stored in TSV(tab separated values) file.
There are multiple ways of storing information in files and the in a higher place ones are some of the most used formats for storing numerical information. To attain our required functionality numpy's loadtxt() part volition be used.
Syntax: numpy.loadtxt(fname, dtype='bladder', comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
Parameters:
fname : File, filename, or generator to read. If the filename extension is .gz or .bz2, the file is first decompressed. Note that generators should return byte strings for Python 3k.
dtype : Data-type of the resulting assortment; default: bladder. If this is a structured data-blazon, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array.
delimiter : The string used to separate values. By default, this is any whitespace.
converters : A dictionary mapping column number to a function that will catechumen that column to a float. E.yard., if column 0 is a appointment string: converters = {0: datestr2num}. Default: None.
skiprows : Skip the first skiprows lines; default: 0.Returns: ndarray
Approach
- Import module
- Load file
- Read numeric information
- Print information retrieved.
Given beneath are some implementation for various file formats:
Link to download data files used :
- Link1 : gfg_example1.txt
- Link2 : gfg_example2.csv
- Link3 : gfg_example3.tsv
- Link4 : gfg_example4.csv
Example 1: Reading numerical information from text file
Python3
import
numpy equally np
filename
=
'gfg_example1.txt'
data_collected
=
np.loadtxt(filename)
print
(data_collected)
impress
(
f
'Stored in : {type(data_collected)} and data blazon is : {data_collected.dtype}'
)
Output :
Instance 2: Reading numerical data from CSV file.
Python3
import
numpy every bit np
filename
=
'gfg_example2.csv'
data_collected
=
np.loadtxt(filename, delimiter
=
','
, dtype
=
int
)
impress
(data_collected)
impress
(
f
'Stored in : {type(data_collected)} and data blazon is : {data_collected.dtype}'
)
Output :
Instance 3: Reading from tsv file
Python3
import
numpy as np
filename
=
'gfg_example3.tsv'
data_collected
=
np.loadtxt(filename, delimiter
=
'\t'
)
print
(data_collected)
print
(
f
'Stored in : {type(data_collected)} and data type is : {data_collected.dtype}'
)
Output :
Example 4: Select only particular rows and skip some rows
Python3
import
numpy as np
filename
=
'gfg_example4.csv'
data_collected
=
np.loadtxt(
filename, skiprows
=
1
, usecols
=
[
0
,
1
], delimiter
=
','
)
print
(data_collected)
print
(
f
'Stored in : {type(data_collected)} and data type is : {data_collected.dtype}'
)
Output :
How To Read Data From A File And Save It As An Array,
Source: https://www.geeksforgeeks.org/how-to-read-a-numerical-data-or-file-in-python-with-numpy/
Posted by: wagnergear1974.blogspot.com
0 Response to "How To Read Data From A File And Save It As An Array"
Post a Comment