How to Read .data File in Python
Summary: in this tutorial, you lot larn various means to read text files in Python.
TL;DR
The post-obit shows how to read all texts from the readme.txt
file into a string:
with open('readme.txt') equally f: lines = f.readlines()
Lawmaking language: JavaScript ( javascript )
Steps for reading a text file in Python
To read a text file in Python, you lot follow these steps:
- First, open a text file for reading past using the
open()
function. - Second, read text from the text file using the file
read()
,readline()
, orreadlines()
method of the file object. - Tertiary, close the file using the file
close()
method.
1) open() role
The open()
function has many parameters but you'll be focusing on the starting time two.
open(path_to_file, manner)
The path_to_file
parameter specifies the path to the text file.
If the file is in the same folder as the plan, you lot just need to specify the name of the file. Otherwise, you demand to specify the path to the file.
To specify the path to the file, y'all utilise the forward-slash ('/'
) even if you lot're working in Windows.
For instance, if the file is readme.txt
stored in the sample folder as the programme, you lot demand to specify the path to the file equally c:/sample/readme.txt
The manner
is an optional parameter. It's a string that specifies the fashion in which yous desire to open the file.
The following table shows available modes for opening a text file:
Manner | Description |
---|---|
'r' | Open for text file for reading text |
'w' | Open a text file for writing text |
'a' | Open a text file for appending text |
For example, to open a file whose proper noun is the-zen-of-python.txt
stored in the same folder as the programme, you lot use the following code:
f = open('the-zen-of-python.txt','r')
Code language: JavaScript ( javascript )
The open()
office returns a file object which you will use to read text from a text file.
2) Reading text methods
The file object provides you with iii methods for reading text from a text file:
-
read()
– read all text from a file into a cord. This method is useful if you take a modest file and you want to manipulate the whole text of that file. -
readline()
– read the text file line by line and return all the lines every bit strings. -
readlines()
– read all the lines of the text file and render them as a list of strings.
iii) close() method
The file that you open will remain open until you close it using the close() method.
It'south important to close the file that is no longer in utilize. If you don't shut the file, the program may crash or the file would be corrupted.
The following shows how to call the close()
method to close the file:
f .shut()
Code linguistic communication: CSS ( css )
To shut the file automatically without calling the close()
method, yous use the with
statement like this:
with open(path_to_file) as f: contents = f.readlines()
Code language: JavaScript ( javascript )
In practice, y'all'll apply the with
statement to shut the file automatically.
Reading a text file examples
We'll use the-zen-of-python.txt file for the demonstration.
The post-obit instance illustrates how to utilize 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') equally f: contents = f.read() print(contents)
Lawmaking language: JavaScript ( javascript )
Output:
Beautiful is amend than ugly. Explicit is better than implicit. Simple is better than complex. ...
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 += ane print(f'line {count}: {line}')
Code linguistic communication: JavaScript ( javascript )
Output:
line 1: Beautiful is better than ugly. line 2: Explicit is ameliorate than implicit. line iii: Uncomplicated is improve than circuitous. ...
The following example shows how to utilise the readline()
to read the text file line past line:
with open('the-zen-of-python.txt') equally f: line = f.readline() while line: line = f.readline() impress(line)
Code language: JavaScript ( javascript )
Output:
Explicit is better than implicit. Uncomplicated is amend than complex. Circuitous is ameliorate than complicated. ...
A more concise way to read a text file line past line
The open()
function returns a file object which is an iterable object. Therefore, you can use a for
loop to iterate over the lines of a text file equally follows:
with open up('the-zen-of-python.txt') as f: for line in f: print(line)
Lawmaking language: JavaScript ( javascript )
This is more concise way to read a text file line past line.
Read UTF-8 text files
The code in the previous examples works fine with ASCII text files. However, if you're dealing with other languages such as Japanese, Chinese, and Korean, the text file is not a elementary 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, you lot demand to laissez passer the encoding='utf-8'
to the open()
office to instruct it to look UTF-8 characters from the file.
For the sit-in, you'll use the following 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
- Utilize the
open()
function with the'r'
mode to open a text file for reading. - Employ the
read()
,readline()
, orreadlines()
method to read a text file. - Ever close a file after completing reading it using the
close()
method or thewith
argument. - Use the
encoding='utf-viii'
to read the UTF-viii text file.
Did you observe this tutorial helpful ?
Source: https://www.pythontutorial.net/python-basics/python-read-text-file/
0 Response to "How to Read .data File in Python"
Post a Comment