Create an array from a .txt file in Python (Copy words from .txt file and put them as array elements)

Well, in this tutorial, you will learn how to copy data from a .txt file and put them as array elements in Python.

Python has become one of the most popular languages today for managing data and its analysis. However, the data that you need usually comes in different file formats. Such data may need to be imported into your program for different purposes.

We will be using Python’s NumPy library which provides ndarray options and various methods to efficiently operate on them.
The NumPy library provides two common methods for importing text files into NumPy arrays.
They are:

  • numpy.loadtxt()
  • numpy.genfromtxt()

Once you import the necessary packages and set the working directory for your program, you can use any of the above two methods depending on your need.

Let us now understand both of them in detail.

numpy.loadtxt() in Python

It loads data from a text file into an array object.

Syntax:

numpy.loadtxt(fname,dtype) , where fname is the name of the text file and dtype allows you to specify the data type.

Let us now look into an example:

Create an array from a .txt file in Python

import numpy as np
datafromfile=np.loadtxt("example.txt",dtype="str")
print(datafromfile)
['apple' 'mango' 'grapes' 'orange' 'banana' 'kiwi']

Important Note:

Each row of the text file must contain the same number of values. Otherwise, it throws an error as shown;

https://drive.google.com/file/d/1QBFurPSiXkh-6gMCN0l7LdvNMKFARKQ_/view?usp=sharing

import numpy as np
datafromfile=np.loadtxt("example.txt",dtype="str")
print(datafromfile)
ValueError: Wrong number of columns at line 2

Detailed Syntax:

numpy.loadtxt(fname,dtype<class ‘float’>, comments=’#’, delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding=’bytes’, max_rows=None, *, like=None)

Except for the filename(fname), all other parameters are optional and you can use them based on your specific needs.

numpy.genfromtxt() in Python

It is the same as loadtxt() except for the fact that it handles missing values too.

Syntax:

numpy.genfromtxt(fname, dtype=float, comments=’#’, delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None,excludelist=None, deletechars=” !#$%&'()*+, -./:;<=>?@[\\]^{|}~”, replace_space=’_’, autostrip=False, case_sensitive=True, defaultfmt=’f%i’, unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None, encoding=’bytes’, *, like=None)

Even here, all parameters except the filename(fname) are optional only.
Let us look into an example for the same below;

numpy.genfromtxt() in Python

import numpy as np
datafromfile=np.genfromtxt("example.txt",dtype="str",delimiter=",")
print(datafromfile)

[[‘apple’ ‘mango’] [‘grapes’ ‘orange’] [‘banana’ ‘kiwi’]]

You must know that both the above methods are very similar and perform the same functions.
However, using genfromtxt() gives you options for dealing with missing values, CSV’s etc.

You can read more about the same at the NumPy documentation here:https://numpy.org/doc/stable/reference/generated/numpy.genfromtxt.html#numpy.genfromtxt

Also, read! Python Array Module

Leave a Reply

Your email address will not be published. Required fields are marked *