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.

with open('text_file.txt') as fl: for line in fl: do_some_processing(line)
Code language: JavaScript (javascript)

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)
Code language: PHP (php)
Content Protection by DMCA.com

Download HitXP Mobile App

Get it on Google Play
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

How does GOD helps us?

Do prayers really help? Do miracles happen when we are in trouble? Does God really help us in times of need? Are prayers enough to save us during bad times? How should we face problems?

Sanskrit poem on Ramayana – read in reverse becomes Mahabharata!

A Sanskrit poem about Ramayana when read in reverse direction becomes a poem on Mahabharata!

The difference between Itihasa and Puranas

Documentation of ancient Indian history and the historical events of Indian civilization in the form of Itihasa and Puranas - Ramayana and Mahabharata.

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.

Latest Music Notations

Oo Antava Mava – Pushpa – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Oo Antava Mava from the 2022 Telugu movie Pushpa in Western and Indian Notations.

Kaa Chalige Bangalore (Tiningaa Miningaa Tishaaaa) – Salaga – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Tiningaa Miningaa Tishaaaa (Kaa Chali Ge) from the 2022 Kannada movie Salaga in Western and Indian Notations.

Title Track BGM – Irul – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Title Track BGM from the 2021 Malayalam movie Irul in Western and Indian Notations.

Ninnukori Varnam – Agni Natchathiram – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Ninnukori Varnam from the 1988 Tamil movie Agni Natchathiram in Western and Indian Notations.
Content Protection by DMCA.com