Reading a text file into a list in Python
This tutorial will examine various methods for loading or reading text files into Python lists. It includes using the function open()
on the file object returned by read().split()
the function, NumPy
the library's loadtxt
function, and csv.reader
the function to load a text file and split it into separate elements in a list.
Read a text file into a list in Python using the file object returned read().split()
by the functionopen()
The code example given below shows how we can first open
read a text file using and then read().split()
split it into an ,
array with as the delimiter using function.
Assume that the content of the text file file.txt
is as follows.
1,2,321,355,313
Code:
with open("file.txt", "r") as tf:
lines = tf.read().split(",")
for line in lines:
print(line)
Output:
1
2
321
355
313
split()
The argument to the function, in this case ,
, specifies the delimiter in the text file.
Read a text file into a list
in Python using NumPy
the library'sloadtxt
The code example given below shows how we can use the function NumPy
of the library loadtxt
to load a text file and delimiter
split it into an array using the parameter.
from numpy import loadtxt
lines = loadtxt("file.txt", delimiter=",")
for line in lines:
print(line)
Output:
1.0
2.0
321.0
355.0
313.0
csv.reader()
Read a text file into a list
using the function in Python
csv
The module is usually used to process CSV files, but can also be used to process text files.
csv
The module's reader
function reads the given file and returns a _csv.reader
object. We can convert the object into a list by applying list()
the function _csv.reader
.
Note that even though the file has only one line, the converted list is a 2D array; therefore, we need to use indexing [0]
to get the 1D list.
import csv
with open("file.txt") as f:
line = csv.reader(f, delimiter=",")
print(list(line)[0])
Output:
['1', '2', '321', '355', '313']
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding a string in a list in Python
Publish Date:2025/05/09 Views:75 Category:Python
-
This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem
Getting list shape in Python
Publish Date:2025/05/09 Views:139 Category:Python
-
In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim
Adding multiple elements to a list in Python
Publish Date:2025/05/09 Views:180 Category:Python
-
List is a mutable data structure in Python. It can contain values of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi
Get the index of the maximum and minimum values in a list in Python
Publish Date:2025/05/09 Views:117 Category:Python
-
In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi
List of numbers from 1 to N in Python
Publish Date:2025/05/09 Views:116 Category:Python
-
This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until
Convert List to Pandas DataFrame in Python
Publish Date:2025/05/09 Views:133 Category:Python
-
This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i
Sorting a list by another list in Python
Publish Date:2025/05/09 Views:148 Category:Python
-
Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python. We will learn how to sort a given list based on the values in another list in this art
Normalizing a list of numbers in Python
Publish Date:2025/05/09 Views:134 Category:Python
-
Normalization means converting the given data to another scale. We rescale the data so that it is between two values. Most of the time, the data is rescaled between 0 and 1. We rescale data for different purposes. For example, machine learn
How to create a list of a specific size in Python
Publish Date:2025/05/09 Views:195 Category:Python
-
Preallocating storage for a list or array is a common practice among programmers when they know the number of elements in advance. Unlike C++ and Java, in Python you must initialize all preallocated storage with some value. Typically, devel