How to Read From Dictionary in Python
Read File Into Lexicon in Python
- Utilize the
split()
Function to Read a File Into a Dictionary in Python - Use the
strip()
Function Along With thesplit()
Function to Read a File Into a Dictionary in Python - Use Dictionary Comprehension to Read a File Into a Dictionary in Python
- Utilize
pandas
Library to Read a File Into a Dictionary in Python
File handling is a vital part of the development and maintenance of any web application. Like other popular programming languages, Python is perfectly capable of supporting file handling. It allows the users to operate on different types of files while carrying some basic operations like reading and writing forth with the other mainstream operations.
This tutorial demonstrates the different ways to read a file into a lexicon in Python.
For reference, we will be using a text file in the code to explain the different methods used in the article.
Contents of the file File1.txt
:
4 10 5 y vi z
Use the dissever()
Function to Read a File Into a Dictionary in Python
The split()
function is generally utilized to chop a given cord into a list.
The post-obit code uses the split()
office to read a file into a dictionary in Python.
a = {} with open("File1.txt") as f: for line in f: (k, v) = line.split() a[int(k)] = five impress(a)
The in a higher place code provides the following output:
{4: 'ten', v: 'y', half dozen: 'z'}
Caption:
- An empty dictionary
a
is created beginning. - The
open()
part is utilized to open and read from the given fileFile1.txt
- The contents of the file are read line by line.
- The line contents are then chopped using the
split up()
function at space character. The character earlier the space is taken as the fundamental while the grapheme after the space is taken as the value of the dictionary. - The
for
loop is utilized for the iteration purpose and for reaching the end of the file.
Use the strip()
Office Along With the divide()
Role to Read a File Into a Dictionary in Python
The strip()
function in Python removes any particularly specified characters or blank spaces at the start and the terminate of a string. The part returns a new string instead of making changes to the original ane.
The following code uses the strip()
function and the split()
function to read a file into a lexicon in Python.
with open('File1.txt') as f: a = dict(i.rstrip().carve up(None, 1) for i in f) print(a)
The above code provides the following output:
{4: 'x', 5: 'y', half-dozen: 'z'}
Explanation:
- An empty lexicon
a
is created kickoff. - The
open()
part is utilized to open and read from the given fileFile1.txt
- The contents of the file are read line by line.
- The line contents are then chopped using the
split()
function at space graphic symbol. Thestrip()
function is also utilized inside the same to remove mentioned characters. - The
for
loop is utilized for the iteration purpose and for reaching the end of the file.
Use Lexicon Comprehension to Read a File Into a Lexicon in Python
The lexicon comprehension is a syntactical extension of the much popular and used listing comprehension.
While dictionary comprehension is syntactically deployed similarly to list comprehension in the Python lawmaking, it has a big difference as the former produces the output every bit a lexicon, unlike the latter, which provides a list
as an output.
The following code uses the dictionary comprehension to read a file into a dictionary in Python.
with open("File1.txt") every bit f: a = {int(grand): 5 for line in f for (k, v) in [line.strip().split(None, 1)]} impress(a)
The above code provides the post-obit output:
{iv: '10', 5: 'y', 6: 'z'}
Apply pandas
Library to Read a File Into a Lexicon in Python
Pandas is a library provided by Python that is utilized for information analysis and manipulation. Pandas is an open-source, easy-to-apply, and flexible library.
The following lawmaking uses the pandas
library to read a file into a dictionary in Python.
import pandas as pd a = pd.read_csv("File1.txt", delimiter=" ", header = None).to_dict()[0] print(a)
The higher up code provides the post-obit output:
{4: '10', 5: 'y', 6: 'z'}
Write for us
DelftStack articles are written by software geeks similar you. If you likewise would similar to contribute to DelftStack past writing paid manufactures, you can check the write for us folio.
Related Commodity - Python File
Related Article - Python Dictionary
Source: https://www.delftstack.com/howto/python/python-read-file-into-dictionary/
0 Response to "How to Read From Dictionary in Python"
Post a Comment