Python Read File Line by Line to Int
Summary: in this tutorial, you lot learn various ways to read text files in Python.
TL;DR
The following shows how to read all texts from the readme.txt
file into a string:
with open('readme.txt') as f: lines = f.readlines()
Code language: JavaScript ( javascript )
Steps for reading a text file in Python
To read a text file in Python, y'all follow these steps:
- First, open a text file for reading past using the
open()
part. - 2d, read text from the text file using the file
read()
,readline()
, orreadlines()
method of the file object. - Third, close the file using the file
shut()
method.
ane) open() function
The open up()
function has many parameters just you lot'll be focusing on the starting time two.
open(path_to_file, mode)
The path_to_file
parameter specifies the path to the text file.
If the file is in the same folder as the program, you just need to specify the proper name of the file. Otherwise, you lot need to specify the path to the file.
To specify the path to the file, you use the forward-slash ('/'
) even if y'all're working in Windows.
For example, if the file is readme.txt
stored in the sample folder equally the plan, yous demand to specify the path to the file as c:/sample/readme.txt
The mode
is an optional parameter. It'southward a string that specifies the fashion in which y'all desire to open the file.
The following table shows bachelor modes for opening a text file:
Mode | Description |
---|---|
'r' | Open for text file for reading text |
'w' | Open up a text file for writing text |
'a' | Open a text file for appending text |
For example, to open up a file whose proper noun is the-zen-of-python.txt
stored in the aforementioned binder every bit the program, y'all use the following code:
f = open('the-zen-of-python.txt','r')
Lawmaking linguistic communication: JavaScript ( javascript )
The open up()
function returns a file object which y'all volition use to read text from a text file.
2) Reading text methods
The file object provides you lot with iii methods for reading text from a text file:
-
read()
– read all text from a file into a string. This method is useful if yous have a pocket-sized file and you want to dispense the whole text of that file. -
readline()
– read the text file line by line and return all the lines as strings. -
readlines()
– read all the lines of the text file and return them as a listing of strings.
3) close() method
The file that you open will remain open until you close information technology using the close() method.
Information technology'south important to close the file that is no longer in use. If y'all don't shut the file, the program may crash or the file would be corrupted.
The post-obit shows how to call the close()
method to close the file:
f .close()
Code linguistic communication: CSS ( css )
To close the file automatically without calling the close()
method, you apply the with
statement like this:
with open up(path_to_file) every bit f: contents = f.readlines()
Code language: JavaScript ( javascript )
In practise, you'll use the with
statement to close the file automatically.
Reading a text file examples
We'll use the-zen-of-python.txt file for the demonstration.
The following example illustrates how to use the read()
method to read all the contents of the the-zen-of-python.txt
file into a string:
with open('the-zen-of-python.txt') as f: contents = f.read() print(contents)
Code language: JavaScript ( javascript )
Output:
Beautiful is better than ugly. Explicit is meliorate than implicit. Simple is better than circuitous. ...
The following example uses the readlines()
method to read the text file and returns the file contents as a list of strings:
lines = [] with open('the-zen-of-python.txt') as f: lines = f.readlines() count = 0 for line in lines: count += 1 print(f'line {count}: {line}')
Code linguistic communication: JavaScript ( javascript )
Output:
line i: Beautiful is better than ugly. line 2: Explicit is better than implicit. line three: Simple is better than complex. ...
The following instance shows how to apply the readline()
to read the text file line by line:
with open('the-zen-of-python.txt') as f: line = f.readline() while line: line = f.readline() print(line)
Code language: JavaScript ( javascript )
Output:
Explicit is amend than implicit. Simple is better than complex. Complex is meliorate than complicated. ...
A more than concise way to read a text file line by line
The open()
role returns a file object which is an iterable object. Therefore, you tin use a for
loop to iterate over the lines of a text file as follows:
with open('the-zen-of-python.txt') as f: for line in f: print(line)
Lawmaking linguistic communication: JavaScript ( javascript )
This is more concise manner to read a text file line by line.
Read UTF-8 text files
The lawmaking in the previous examples works fine with ASCII text files. All the same, if you're dealing with other languages such equally Japanese, Chinese, and Korean, the text file is not a simple ASCII text file. And it's likely a UTF-8 file that uses more than just the standard ASCII text characters.
To open a UTF-8 text file, y'all need to pass the encoding='utf-8'
to the open()
part to instruct information technology to expect UTF-8 characters from the file.
For the demonstration, you'll use the post-obit quotes.txt
file that contains some quotes in Japanese.
The following shows how to loop through the quotes.txt
file:
with open('quotes.txt', encoding='utf8') as f: for line in f: print(line.strip())
Code language: JavaScript ( javascript )
Output:

Summary
- Use the
open()
function with the'r'
mode to open up a text file for reading. - Employ the
read()
,readline()
, orreadlines()
method to read a text file. - Always close a file afterward completing reading it using the
close()
method or thewith
statement. - Employ the
encoding='utf-8'
to read the UTF-viii text file.
Did you discover this tutorial helpful ?
Source: https://www.pythontutorial.net/python-basics/python-read-text-file/
Post a Comment for "Python Read File Line by Line to Int"