Making a Dictionary from Each Line in a File

How to make a dictionary entry from each line in a text file?

My current approach uses file handling to open a file in read mode, and then reading the lines present in the file. Then for each line, remove extra new line and whitespaces and split it at space, to create a list. Then used unpacking to store single value as key and a list of 2 values as value. Added values to the dictonary.

temp.txt

student_num1 student_name1 student_grade1
student_num2 student_name2 student_grade2
student_num3 student_name3 student_grade3

main.py

d = dict()
with open("temp.txt", "r") as f:
for line in f.readlines():
key, *values = line.strip().split(" ")
d[key] = values
print(d)

Output

{'student_num1': ['student_name1', 'student_grade1'], 'student_num2': ['student_name2', 'student_grade2'], 'student_num3': ['student_name3', 'student_grade3']}

python: create dictionary from lines of txt file

You can define a new variable line_num that keeps track of line numbers

def getFile():
prose = str(input('Please enter the file path for your text file: '))

dictionary = {}

infile = open(prose, 'r')
line_num = 1
for line in infile:
dictionary[line_num] = line
line_num += 1
print(dictionary)
infile.close()

getFile()

You can view the dictionary with line numbers as keys and each line as their values

Output:

{1: 'roses are red\n', 2: 'violets are blue'}

How do I read lines from a file and create a dictionary from them?

This code first reads the file line by line and then splits each line into a 2d array so it looks like this:

[[a, b],
[c, d]
...
]

It adds dictionary entries based on this array (mydict[a] = b and so on).

with open("filename", "r") as file:
myinput = file.readlines()
myinput = [i.split(" ") for i in myinput]
mydict = {}
for i in myinput:
mydict[i[0]] = i[1]

As @wjandrea pointed out, you can also write the above code like this:

with open("filename", "r") as file:
mydict = {line.split() for line in file}

Reading from text file. Making it to a dictionary

You are very close. The line variable is what you need:

content = []
with open ("sample.txt","r",encoding="utf-8") as file:
for line in file:
line = line.strip().split(" ")
mydict = {"A":int(line[0]),
"B":int(line[1]),
"C":int(line[2])}
content.append(mydict)

print (content)

How to create a dictionary of lists from each row of a text file?

This is a bit more simple.

d = {} # this is the dictonary
file = open("data.txt") # open file
for line in file:
s = line.split() # split line
# get data individually
key = s[0]
fullname = s[1] + ' ' + s[2]
year = s[3]
# append to dictionary
d[key] = [fullname, year]
# print
print(d)

hope this works

Making a dictionary from each line in a file

Edited answer, improved thanks to the comments from @PM2Ring and @IljaEverilä.

Here is my original solution using a dictionary comprehension

followers = {line.split()[0]: line.split()[1:] for line in follows_file}

A more efficient alternative proposed by @IljaEverilä, which avoids calling split twice, is:

followers = {follower: followees for follower, *followees in map(str.split, follows_file)}

Result:

{'andrew': ['fred'],
'fred': [],
'george': ['judy', 'andrew'],
'john': ['george'],
'judy': ['andrew', 'fred']}

Note that both of the above solutions assume that your file contains no duplicate keys.

Don't forget to close your file afterwards:

follows_file.close()

Or better, just use a context manager, which handles the file closing for you:

with open('C:\\Users\\zacan\\Desktop\\Python\\follows.txt', 'r') as follows_file:
followers = {follower: followees for follower, *followees in map(str.split, follows_file)}

Writing each value in dictionary to text file

d = {"a": ["1", "2", "3"], "b": ["1"]}

for key, value in d.items():
filename = str(os.path.join(dir) + key + '.txt')
with open(filename, "w") as f:
for v in value:
f.write(v + "\n")

You can loop on the dictionary and create a new file for each key and start writing values on new lines.

How to create a dictionary from a line of text?

In Python 2 you could use izip in the itertools module and the magic of generator objects to write your own function to simplify the creation of pairs of values for the dict records. I got the idea for pairwise() from a similarly named (although functionally different) recipe in the Python 2 itertools docs.

To use the approach in Python 3, you can just use plain zip() since it does what izip() did in Python 2 resulting in the latter's removal from itertools — the example below addresses this and should work in both versions.

try:
from itertools import izip
except ImportError: # Python 3
izip = zip

def pairwise(iterable):
"s -> (s0,s1), (s2,s3), (s4, s5), ..."
a = iter(iterable)
return izip(a, a)

Which can be used like this in your file reading for loop:

from sys import argv

records = {}
for line in open(argv[1]):
fields = (field.strip() for field in line.split(',')) # generator expr
record = dict(pairwise(fields))
records[record['TSN']] = record

print('Found %d records in the file.' % len(records))

But wait, there's more!

It's possible to create a generalized version I'll call grouper(), which again corresponds to a similarly named itertools recipe (which is listed right below pairwise()):

def grouper(n, iterable):
"s -> (s0,s1,...sn-1), (sn,sn+1,...s2n-1), (s2n,s2n+1,...s3n-1), ..."
return izip(*[iter(iterable)]*n)

Which could be used like this in your for loop:

    record = dict(grouper(2, fields))

Of course, for specific cases like this, it's easy to use functools.partial() and create a similar pairwise() function with it (which will work in both Python 2 & 3):

import functools
pairwise = functools.partial(grouper, 2)

Postscript

Unless there's a really huge number of fields, you could instead create a actual sequence out of the pairs of line items (rather than using a generator expression which has no len()):

fields = tuple(field.strip() for field in line.split(','))

The advantage being that it would allow the grouping to be done using simple slicing:

try:
xrange
except NameError: # Python 3
xrange = range

def grouper(n, sequence):
for i in xrange(0, len(sequence), n):
yield sequence[i:i+n]

pairwise = functools.partial(grouper, 2)


Related Topics



Leave a reply



Submit