Home Code How to read a large text file character by character in Python?

How to read a large text file character by character in Python?

Every python programmer knows how to read a text file line by line without loading the entire file into memory using the below simple script.

%MINIFYHTMLfd1ab092bbea7c5297ca4c386d8bf5ea43%
with open('text_file.txt') as fl: for line in fl: do_some_processing(line)

But what if you want to load a text file character by character? Possible use case is when the text file is a huge file with all its contents in a single line without any newline or line breaking character.

So what if the text file is in a single large line, and the size of the file does not fit into your system’s RAM or memory? In that case you will have to read the text file one character at a time using the Python script below.

with open('text_file_with_single_large_line.txt') as fl: while True: #read a huge chunk of characters. #Increase this if you want to read more characters in every single read chars=infile.read(1000000) #no more pending characters in file? if not chars:break #process the data do_some_processing(chars)
Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

Online Education should be interactive and innovative – Fun to Learn

For online education to succeed for smaller classses - it should be as interesting as cartoons. Online education cant succeed unless it is more interesting, interactive and innovative than offline schooling.

The Complete List of Dhatus – Sanskrit Root Words and their English meaning

A complete list of all dhatus (root words or verb roots) in Sanskrit dictionary and their meanings with corresponding IPA transliteration.

Sanskrit Lesson 4 – Word Creation Magic using Dhatus

Learn how easy it is to create new words in Sanskrit using root words called Dhatus. This simple process of Sanskrit grammar powers the entire language.

The Secret Role of Karma in Mahabharata

The seeds of Mahabharata were sown long back Duryodhana or Dhritarashtra were born. The story of Kaurvas and Pandavas is only the second half of the cycle of Karma. So what is the first half?

Latest Music Notations

So Gaya Ye Jahan – Tezaab – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song So Gaya Ye Jahan from the 1988 Hindi movie Tezaab in Western and Indian Notations.

Background Theme (BGM) – Mouna Ragam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Background Theme (BGM) from the 1986 Tamil movie Mouna Ragam in Western and Indian Notations.

Dil Kya Kare Jab Kisi Se – Julie – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Dil Kya Kare Jab Kisi Se from the 1975 Hindi movie Julie in Western and Indian Notations.

Albela Sajan Aayo Re – Hum Dil De Chuke Sanam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Albela Sajan Aayo Re from the 1999 Hindi movie Hum Dil De Chuke Sanam in Western and Indian Notations.
Content Protection by DMCA.com