How to Read Hbase Meta Using Python

Summary: in this tutorial, you larn diverse ways to read text files in Python.

TL;DR

The following shows how to read all texts from the readme.txt file into a cord:

            

with open up('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, you follow these steps:

  • First, open a text file for reading by using the open up() function.
  • Second, read text from the text file using the file read(), readline(), or readlines() method of the file object.
  • Third, close the file using the file close() method.

one) open() function

The open() function has many parameters but you'll be focusing on the start 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 plan, you lot just need to specify the name of the file. Otherwise, you need to specify the path to the file.

To specify the path to the file, you use the frontward-slash ('/') even if you're working in Windows.

For instance, if the file is readme.txt stored in the sample folder as the plan, you need to specify the path to the file as c:/sample/readme.txt

The mode is an optional parameter. Information technology's a cord that specifies the mode in which you desire to open the file.

The post-obit table shows available modes for opening a text file:

Mode Description
'r' Open for text file for reading text
'westward' Open a text file for writing text
'a' Open up 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 binder as the program, you lot use the following code:

            

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

Lawmaking language: JavaScript ( javascript )

The open() part returns a file object which you will utilize to read text from a text file.

2) Reading text methods

The file object provides y'all with three methods for reading text from a text file:

  • read() – read all text from a file into a string. This method is useful if y'all have a small file and yous want to manipulate the whole text of that file.
  • readline() – read the text file line past line and return all the lines as strings.
  • readlines() – read all the lines of the text file and return them every bit a list of strings.

three) close() method

The file that yous open will remain open until you close it using the close() method.

Information technology's important to close the file that is no longer in utilize. If you lot don't close the file, the plan may crash or the file would be corrupted.

The following shows how to call the close() method to close the file:

            

f .shut()

Code language: CSS ( css )

To close the file automatically without calling the shut() method, you use the with statement like this:

            

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

Code linguistic communication: JavaScript ( javascript )

In exercise, you'll use the with statement to close the file automatically.

Reading a text file examples

We'll apply the-zen-of-python.txt file for the demonstration.

The following instance 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 up('the-zen-of-python.txt') as f: contents = f.read() impress(contents)

Code language: JavaScript ( javascript )

Output:

            

Beautiful is amend than ugly. Explicit is improve than implicit. Uncomplicated is amend than complex. ...

The following example uses the readlines() method to read the text file and returns the file contents every bit a list of strings:

            

lines = [] with open('the-zen-of-python.txt') as f: lines = f.readlines() count = 0 for line in lines: count += one print(f'line {count}: {line}')

Code language: JavaScript ( javascript )

Output:

            

line 1: Beautiful is better than ugly. line 2: Explicit is better than implicit. line iii: Elementary is better than complex. ...

The following example shows how to use 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 improve than implicit. Simple is amend than circuitous. Complex is amend than complicated. ...

A more concise way to read a text file line by line

The open up() office returns a file object which is an iterable object. Therefore, yous tin use a for loop to iterate over the lines of a text file as follows:

            

with open('the-zen-of-python.txt') equally f: for line in f: impress(line)

Code language: JavaScript ( javascript )

This is more concise mode 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 y'all'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-eight text file, you need to pass the encoding='utf-8' to the open up() function to instruct it to expect UTF-8 characters from the file.

For the demonstration, 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 up('quotes.txt', encoding='utf8') equally f: for line in f: print(line.strip())

Lawmaking language: JavaScript ( javascript )

Output:

Python read utf-8 text file

Summary

  • Use the open() part with the 'r' mode to open up a text file for reading.
  • Utilize the read(), readline(), or readlines() method to read a text file.
  • Always close a file afterward completing reading it using the close() method or the with statement.
  • Use the encoding='utf-eight' to read the UTF-viii text file.

Did yous observe this tutorial helpful ?

jonesforienionly.blogspot.com

Source: https://www.pythontutorial.net/python-basics/python-read-text-file/

0 Response to "How to Read Hbase Meta Using Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel