write the values of an ordered dictionary into a file

MRAB python at mrabarnett.plus.com
Thu Jun 21 13:48:19 EDT 2018


On 2018-06-21 18:11, Ganesh Pal wrote:
 > Hi Team
 >
 > I need to write the values of an ordered dictionary into a file . All
 > values should be in a single row with a header list
 >
 > *Example:*
 >
 > *student = [("NAME", "John"),*
 > *           ("AGE", 28),*
 > *           ("SCORE", 13),*
 > *           ("YEAR", 2018),*
 > *           ("FEE", 250)]*
 > *student = OrderedDict(student)*
 >
 > *The OrderedDict Should be append at the end of the file as as shown 
below.*
 >
 > *# tail -2  /tmp/student_record.txt *
 > *.................................................................*
 > *||STUDENT NAME||STUDENT AGE||MARKS SCORED||PASSED YEAR||FEES PAID||*
 > *||John        ||  28       ||  13        || 2018      || 250     ||*
 >
 >
 > Questions:
 >
 > (1) Below is my partial solution , any comments and suggestions ( I 
am not
 > to get the “||” delimiter correctly, trying it )
 >
 > #!/usr/bin/python
 >
 > # A Python program to write the values of an OderedDict into a file
 > # The values should be formatted correctly under its headers
 >
 > from collections import OrderedDict
 > tmp = '/tmp/student_record.txt'
 >
 > student = [("NAME", "John"),
 >             ("AGE", 28),
 >             ("SCORE", 13),
 >             ("YEAR", 2018),
 >             ("FEE", 250)]
 >
 > student = OrderedDict(student)
 >
 > header_list = ["STUDENT NAME", "STUDENT AGE", "MARKS SCORED", "PASSED 
YEAR",
 >                  "FEES PAID"]
 >
 > header_string = '||' + '||'.join(header_list) + '||'
 >
 > with open(tmp, 'a') as fd:

header_string is already a string, so here you're just iterating over 
the characters of that string:

 >       for item in header_string:
 >           fd.write("%s" % (item))
 >
You can write it more simple like this:

fd.write("%s\n" % header_string)

Here you're writing a newline before each of the fields/columns:

 >       for value in student.values():
 >           fd.write("\n")
 >           fd.write("||")
 >           fd.write("%s" % (value))
 >
 >
 > *output:*
 >
 > *root at X1:/Play_ground/SPECIAL_TYPES# cat /tmp/student_record.txt*
 > *||STUDENT NAME||STUDENT AGE||MARKS SCORED||PASSED YEAR||FEES PAID||*
 > *||John*
 > *||28*
 > *||13*
 > *||2018*
 >
 >
 > (2)Any alternative way to solve this easily and store the data  in the
 > requested format (can I still use cvs writer to format this)
 >
 >
 > I am a Linux user with Python 2.7
 >
What I would do is measure the widths of the each heading and of each 
value and then pad them out to the longest:


header_list = ["STUDENT NAME", "STUDENT AGE", "MARKS SCORED", "PASSED 
YEAR", "FEES PAID"]
header_widths = [len(header) for header in header_list]

value_list = [str(value) for value in student.values()]
value_widths = [len(value) for value in value_list]

column_widths = [max(header_width, value_width) for header_width, 
value_width in zip(header_widths, value_widths)]

with open(tmp, 'a') as fd:
     fd.write('||' + '||'.join(header.ljust(width) for header, width in 
zip(header_list, column_widths)) + '||\n')
     fd.write('||' + '||'.join(value.ljust(width) for value, width in 
zip(value_list, column_widths)) + '||\n')



More information about the Python-list mailing list