×

Python Parse Text File

We will learn different ways of read text records in Python.

TL;DR

The accompanying tells the best way to read all texts from the readme.txt document into a string:

with open('readme.txt') as f:

lines = f.readlines()

Steps For reading a Text Document in Python

  • To read a text document in Python, you follow these means:
  • In the first place, open a text record for reading by utilizing the open() capability.
  • Second, read text from the text document utilizing the record read(), readline(), or readlines() technique for the record object.
  • Third, close the record utilizing the document close() technique.
  1. open() function:

The open() function has numerous boundaries however you'll zero in on the initial two:

open(path_to_file, mode)

The path_to_file boundary indicates the way to the text record.

Assuming that the program and record are in a similar envelope, you really want to determine just the filename of the document.

In any case, you really want to incorporate the way to the document as well as the filename.

To determine the way to the record, you utilize the forward-slice ('/') regardless of whether you're dealing with Windows.

For instance, if the document readme.txt is put away in the example envelope as the program, you want to determine the way to the record as c:/example/readme.txt

The mode is a discretionary boundary. A string determines the mode wherein you need to open the document.

 The accompanying table shows accessible modes for opening a text record:

Mode and Description

  • 'r’ means Open for text record for understanding text
  • 'w' means Open a text record for composing text
  • 'a' means Open a text record for affixing text

For instance, to open a document whose name is the-harmony of-python.txt put away in a similar envelope as the program, you utilize the accompanying code:

f = open('the-harmony of-python.txt','r')

The open() capability returns a record object which you will use to peruse text from a text document.

2. reading  text strategies

The document object furnishes you with three strategies for reading text from a text record:

  • read(size) - read a few items in a record in view of the discretionary size and return the items as a string. On the off chance that you overlook the size, the read() technique peruses from where it left off till the finish of the document. In the event that the finish of a document has been reached, the read() strategy returns an unfilled string.
  • readline() - read a solitary line from a text record and return the line as a string. On the off chance that the finish of a record has been reached, the readline() returns an unfilled string.
  • readlines() - read every one of the lines of the text record into a rundown of strings. This strategy is helpful to control the entire text of that record.

3. close() technique

The record that you open will stay open until you close it utilizing the nearby() strategy.

It's vital to close the record that is presently not being used for the accompanying reasons:

In the first place, when you open a document in your content, the record framework typically secures it so no different projects or scripts can utilize it until you close it.

Second, your record framework has a predetermined number of document descriptors that you can make before it runs out of them.

Although this number may be high, it's feasible to open a ton of documents and exhaust your record framework assets.

Third, leaving many records open might prompt race conditions which happen when various cycles endeavor to alter one document simultaneously and can cause a wide range of startling ways of behaving.

The accompanying tells the best way to call the nearby() technique to close the record:

f.close()

To close the document consequently without calling the nearby() technique, you utilize the with explanation like this:

with open(path_to_file) as f:
    contents = f.readlines()

By and by, you'll utilize with articulation to naturally close the document.

Reading a text document models:

  • We'll utilize the-harmony of-python.txt document for the exhibition.
  • The accompanying model outlines how to utilize the read() strategy to peruse every one of the items in the-harmony of-python.txt document into a string:
withopen('the-harmony of-python.txt') as f:
    contents = f.read()
    print(contents)

Output:

Wonderful is better compared to revolting.
Express is better compared to certain.
Basic is superior to complex.
  • The accompanying model purposes the readlines() strategy to peruse the text record and returns the document contents as a rundown of strings:
with open('the-harmony of-python.txt') as f:
    [print(line) for line in f.readlines()]

Output:

Delightful is better compared to appalling.
Express is better compared to implied.
Straightforward is superior to complex.
Complex is better compared to convoluted.
  • The explanation you see a clear line after each line from a record is that each line in the text document has a newline character (\n).
  • To eliminate the clear line, you can utilize the strip() technique. For instance:
with open('the-harmony of-python.txt') as f:
    [print(line.strip()) for line in f.readlines()]
  • The accompanying model tells the best way to utilize the readline() to peruse the text record line by line:
with open('the-harmony of-python.txt') as f:
    while Valid:
        line = f.readline()
        on the off chance that not line:
            break
        print(line.strip())

Output:

Unequivocal is better compared to verifiable.
Complex is better compared to muddled.
Level is better compared to settled.

A more brief method for perusing a text document line by line:

  • The open() capability returns a record object which is an iterable item. Subsequently, you can utilize a for circle to emphasize over the lines of a text document as follows:
with open('the-harmony of-python.txt') as f:
    for line in f:
  • This is a more succinct method for perusing a text record line by line.

Peruse UTF-8 Text Records:

  • The code in the past models turns out great with ASCII text records. In any case, in the event that you're managing different dialects like Japanese, Chinese, and Korean, the text record is definitely not a basic ASCII text document. Furthermore, it's probable an UTF-8 document that utilizes something beyond the standard ASCII text characters.
  • To open an UTF-8 text record, you really want to pass the encoding='utf-8' to the open() capability to train it to anticipate UTF-8 characters from the document.
  • For the show, you'll utilize the accompanying quotes.txt record that contains a few statements in Japanese.
  • The accompanying tells the best way to circle through the quotes.txt record:
with open('quotes.txt', encoding='utf8') as f:
    for line in f:
        print(line.strip())

Conclusion:

  • Utilize the open() capability with the 'r' mode to open a text document for perusing.
  • Utilize the read(), readline(), or readlines() strategy to peruse a text document.
  • Continuously close a document in the wake of finishing perusing it utilizing the nearby() technique or the with explanation.
  • Utilize the encoding='utf-8' to peruse the UTF-8 text document

Related Topics

How to Convert int to str in Python

How to Convert int to str in Python In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available...

4 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

How to get Time in seconds in Python

Python's time module source shows that are based on time. The time() method is used by developers and returns the current time as a UNIX timestamp. A UNIX timestamp is...

4 minutes read.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Bar Plot in Python

A bar chart or bar graph displays categorical data using rectangular bars with heights or lengths proportionate to the values for the data distributed in the dataset. In a bar...

16 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Difference between Yield and Return in Python

Python yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns...

3 minutes read.

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as...

3 minutes read.

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Decision Tree Classification in Python

In this article, we will learn the implementation of the decision tree in Sklearn, which is nothing but the Scikit Learn library of python. First, we should learn what classification...

12 minutes read.

Python program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.