 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a Python dictionary from text file?
Assuming a following text file (dict.txt) is present
1 aaa
2 bbb
3 ccc
Following Python code reads the file using open() function. Each line as string is split at space character. First component is used as key and second as value
d = {}
with open("dict.txt") as f:
for line in f:
    (key, val) = line.split()
    d[int(key)] = val
print (d)
The output shows contents of file in dictionary form
{1: 'aaa', 2: 'bbb', 3: 'ccc'}Advertisements
                    