Python Forum
Fetch student details from a text file and print the details.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fetch student details from a text file and print the details.
#1
I am getting the desired output for this problem but feel that my code is very long. Please suggest if its possible to shorten the code and make it better.

METHOD USED

1.The details of the students who scored "B" grade are fetched from file.txt and then written in another text file temp.txt.
2.After that ,the data is stored in a list of lists.
3.Finally the details are stored in a dictionary and the output is displayed.


In this problem, the student details are present in a text file as shown below. It is in the format (name,grade,city)

INPUT FILE DATA:(file.txt)
Shankar,A,Chennai
Arun,B,Chennai
Karthik,A,Chennai
Raj,B,Chennai
Ram,A,Chennai

Now we need to fetch and print the details of the students who scored "B" grade in the below format.

OUTPUT:
Student 1
Student name : Arun
Grade : B
Location : Chennai
Student 2
Student name : Raj
Grade : B
Location : Chennai

from collections import OrderedDict
grade=input("Enter the grade to search\n")
lines=0

"""Write data with the specific grade to another text file"""

with open("file.txt","r") as f1:
    with open("temp.txt","w") as f2:    
        for i in f1:
            new=i.split(",")
            for k in range(0,len(new)):
                if str(new[k])==grade:
                    f2.write(i)
f1.close()
cnt=0
new=[]      							#Will store data as list of lists

"""The data is stored in a list of lists"""

with open("temp.txt","r") as f2:
    for i in f2:
        cnt+=1							#Number of lines in the temp.txt file
        i=i.split(",")
        new.append(i)

k=0
while(k<cnt):
    
    print "Student 1"
    dd1=OrderedDict()
    dd1["Student name:"]= new[0][0]     #Access elements of a list of lists
    dd1["Grade:"]=new[0][1]
    dd1["Location:"]=new[0][2]        
    for x,y in dd1.items():
        print (x,y)
    k+=1
    if k==cnt:
        break

    print "Student 2"    
    dd2=OrderedDict()
    dd2["Student name:"]= new[1][0]     #Access elements of a list of lists    
    dd2["Grade:"]=new[1][1]
    dd2["Location:"]=new[1][2]        
    for x,y in dd2.items():
        print (x,y)
    k+=1
    if k==cnt:
        break

    print "Student 3"
    dd3=OrderedDict()
    dd3["Student name:"]= new[2][0]     #Access elements of a list of lists
    dd3["Grade:"]=new[2][1]
    dd3["Location:"]=new[2][2]        
    for x,y in dd3.items():
        print (x,y)
    k+=1
    if k==cnt:
        break
Reply
#2
I think this should work. I haven't tested it, but it should do the same without saving temporary results to disk.

import csv


seeking_grade = input("Enter the grade to search\n")
# is a str

with open("file.txt") as fd:
    reader = csv.reader(fd)
    for index, (student, grade, location) in enumerate(reader, start=1):
        if grade == seeking_grade:
            print("Student", index)
            print("Student name:", student)
            print("Grade:", grade)
            print("Location:", location)
Pranav likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
The code is working fine. Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Student project - alert action when X happens Y amt of times, how? unknown00 2 1,815 Aug-25-2021, 08:07 PM
Last Post: namarang
  Student grader and sorter for assignment RazeAD 7 3,223 Feb-11-2021, 06:29 AM
Last Post: RazeAD
  Generating a student's transcript [OOP concept] aongkeko 2 2,720 Dec-01-2020, 06:43 AM
Last Post: buran
  Reading a text until matched string and print it as a single line cananb 1 2,069 Nov-29-2020, 01:38 PM
Last Post: DPaul
  Create code for input names and score for 15 student Davin 2 2,103 Sep-21-2020, 08:49 AM
Last Post: DeaD_EyE
  New Python Student = Does this code look right? musicjoeyoung 6 3,473 May-07-2020, 02:39 PM
Last Post: musicjoeyoung
  How can details be dynamically entered into a list and displayed using a dictionary? Pranav 5 2,983 Mar-02-2020, 10:17 AM
Last Post: buran
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,968 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Convert text from an image to a text file Evil_Patrick 5 4,341 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,529 Dec-11-2018, 02:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020