Tag: Lines
Python: How to delete specific lines in a file in a memory-efficient way?
In this article, we will discuss different ways to delete specific lines from a file either by line number, by matching content or based on any custom logic. In Python, there is no direct API to delete lines or text from the middle of a file. Therefore, in this article, we will follow an approach …
Python: How to delete specific lines in a file in a memory-efficient way?Read More
Python: Search strings in a file and get line numbers of lines containing the string
In this article, we are going to discuss, how to search for single or multiple strings in a file and get all the matched lines along with their line numbers. Check if a string exists in a file To check if a given string exists in the file or not, we have created a function, …
Python: Search strings in a file and get line numbers of lines containing the stringRead More
How to append text or lines to a file in python?
In this article, we will discuss how to append text or new lines to an existing file using python. To append some text to a file in the end, we first need to open the file with access mode ‘a’, file_object = open(‘sample.txt’, ‘a’) With file access mode ‘a’, open() function first checks if file …
Python: How to insert lines at the top of a file?
In this article, we will discuss how to insert single or multiple lines at the beginning of a text or CSV file in python. How to insert a line at the top of a file There is no direct way to insert text in the middle of a file. Therefore we have to create a …
Python: Get last N lines of a text file, like tail command
In this article, we will discuss a memory-efficient solution to read the last line or last N lines from a text or CSV file in python. Then we will also see how to real only last line or check if the last line in the file matches the given line. We have created a function …
Python: Get last N lines of a text file, like tail commandRead More
Split Multi-Line String into multiple Lines in Python
In this Python tutorial we are going to learn about methods through which we can split a multi-line string into multiple lines in Python. Table Of Contents Method 1 : Using splitlines() Method 2 : Using replace() method Summary But first what is a multi-line string ? Advertisements A multi-line string is a string enclosed …
Split Multi-Line String into multiple Lines in PythonRead More
Print a list of lists in separate lines in Python
In this article, we will discuss different ways to print each sublist in a list of lists in separate line in Python. Table Of Contents Introduction Method 1: Using for loop Method 2: By converting into string Summary Introduction Suppose we have a list of lists in Python like this, Advertisements listOfLists = [ [11, …
Print List elements on separate Lines in Python
In this article, we will discuss how to print list elements on separate lines in Python. Table Of Contents Introduction Method 1: Using a for loop Method 2: Using print() function Method 3: Using join() function Example 1 Example 2 Summary Introduction Suppose we have a list of strings like this, Advertisements listOfStrs = [‘Hello’, …