Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4125780
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:54:52+00:00 2026-05-20T23:54:52+00:00

So i have this txt file called Students.txt, I want to define a function

  • 0

So i have this txt file called “Students.txt”, I want to define a function called load(student)
so i have this code:

def load(student):
      body

im not quite sure what to write for the body of code so that it reads the file and returns the value from the file as dictionary. I know it would be something like readlines()
anyway, the file students.txt looks like this:

P883, Michael Smith, 1991
L672, Jane Collins, 1992
(added)L322, Randy Green, 1992
H732, Justin Wood, 1995(added)
^key  ^name        ^year of birth 

the function has to return as a dictionary that looks like this:

{'P883': ('Michael Smith',1991),
'(key)':('name','year')}

I managed to return the values by trial and error however i cant make new lines and keep returning \n.

===============
this question has been answered and i used the following code which works perfectly however when there is a space in the values from the txt file.. (see added parts) it doesnt work anymore and gives an error saying that list index is out of range

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:54:53+00:00Added an answer on May 20, 2026 at 11:54 pm

    Something like this should do it, I think:

    students = {}
    
    infile = open("students.txt")
    for line in infile:
      line = line.strip()
      parts = [p.strip() for p in line.split(",")]
      students[parts[0]] = (parts[1], parts[2])
    

    This might not be 100%, but should give you a starting-point. Error handling was omitted for brevity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file called header.txt I want this file to be prepended (first
I have this page called up.php that adds 1 to a txt file set
I'm creating a Shell Script, and I have a file like this called expressions.txt
I have to read a txt file with lines formated like this: 1: (G,
I have a generated txt file. This file has certain lines that are superfluous,
Let's say you have a text file like this one: http://www.gutenberg.org/files/17921/17921-8.txt Does anyone has
I have this code in jQuery, that I want to reimplement with the prototype
I have this php code: <?php if(!file_exists('counter.txt')){ file_put_contents('counter.txt', '0'); } if($_GET['click'] == 'yes'){ file_put_contents('counter.txt',
I have some R code inside a file called analyse.r. I would like to
Lets say I have a file called foo.txt encoded in utf8: aoeu qjkx ñpyf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.