Function for reading data into a dictionary - Python

Watch
Blaze3211
Badges: 11
Rep:
?
#1
Report Thread starter 7 months ago
#1
Hi

I've been trying to import data from a text file into a dictionary underneath 5 keys which I have created from an empty dictionary.
I found there to be 141 lines in total so I iterated for I in range 142 to cover the lines throughout the text file. I have managed to write some code to do this but the issue I have is where I try to put this into a function as it doesn't seem to work as well.
In the first code I get what I want but when attempting to put it into a function I don't get the whole thing just the first entries. Does anyone know why this is the case and what I could do to resolve this?

I appreciate any help. Many thanks.
Attached files
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 7 months ago
#2
The the reason it only reads the first line is that your code is returning inside the loop, which means it'll stop after the first iteration because the function returns back to the main program after the first iteration.

The indenting of your return statement looks as if it should be at the same level as your 'with' keyword -- that will ensure that your loops complete.

Also a few other observations:

  • Minor nitpick, but I'd recommend you don't use curvy parenthesis around the return value from a function unless you want a tuple. In this case the parenthesis are redundant, but from a point of view of style and consistency, it looks wrong because return is a keyword and not a function. Typical python style would be return dictionary rather than return(dictionary).

  • The "for" line which contains the magic number 142 is unnecessary/redundant, and even though it works, it looks like a logic-error at first glance, and would break if the size of the file changes.
    (There is never any need at all to hard-code the length of a file into a program - it's much easier just to let python figure that out for itself, and then you are also protected against the program failing when the file length changes -- "magic numbers" like that are usually a sign of some brittle code which could easily cause a subtle bug in future).

  • The following line which calls fin.readline() is also unnecessary. You don't need a nested 'for' loop here, because your outer for-loop is already iterating through the file line-by-line. A single for line in fin: is all you need to step through each line in the file.

  • You can skip the first line of the file using next(fin) after opening it to bypass the header row if you need to.

  • Your dictionary is created outside of your function, so it's being treated as a global variable -- you can put the initial creation of the dictionry in the function as well (that would be better). Global variables are something to be avoided ideally because it means the function isn't really self-contained (it has side effects, changing something which doesn't belong to that function), and in larger programs, this kind of thing can cause a lot of 'spaghetti' logic which can be difficult to reason over.

I tried it with some test data here: https://repl.it/repls/UnusualMistyroseAmoebas
Last edited by winterscoming; 7 months ago
1
reply
Blaze3211
Badges: 11
Rep:
?
#3
Report Thread starter 7 months ago
#3
(Original post by winterscoming)
The the reason it only reads the first line is that your code is returning inside the loop, which means it'll stop after the first iteration because the function returns back to the main program after the first iteration.

The indenting of your return statement looks as if it should be at the same level as your 'with' keyword -- that will ensure that your loops complete.

Also a few other observations:

  • Minor nitpick, but I'd recommend you don't use curvy parenthesis around the return value from a function unless you want a tuple. In this case the parenthesis are redundant, but from a point of view of style and consistency, it looks wrong because return is a keyword and not a function. Typical python style would be return dictionary rather than return(dictionary).

  • The "for" line which contains the magic number 142 is unnecessary/redundant, and even though it works, it looks like a logic-error at first glance, and would break if the size of the file changes.
    (There is never any need at all to hard-code the length of a file into a program - it's much easier just to let python figure that out for itself, and then you are also protected against the program failing when the file length changes -- "magic numbers" like that are usually a sign of some brittle code which could easily cause a subtle bug in future).

  • The following line which calls fin.readline() is also unnecessary. You don't need a nested 'for' loop here, because your outer for-loop is already iterating through the file line-by-line. A single for line in fin: is all you need to step through each line in the file.

  • You can skip the first line of the file using next(fin) after opening it to bypass the header row if you need to.

  • Your dictionary is created outside of your function, so it's being treated as a global variable -- you can put the initial creation of the dictionry in the function as well (that would be better). Global variables are something to be avoided ideally because it means the function isn't really self-contained (it has side effects, changing something which doesn't belong to that function), and in larger programs, this kind of thing can cause a lot of 'spaghetti' logic which can be difficult to reason over.

I tried it with some test data here: https://repl.it/repls/UnusualMistyroseAmoebas
Thanks a lot I really appreciate it and have learned a lot from your post. Do you know how I would go about making the input argument the file name which returns the dictionary once called upon. I have tried simply putting the name of the file into the parenthesis but whenever I put something in there I tend to get the invalid syntax error.
0
reply
winterscoming
Badges: 19
Rep:
?
#4
Report 7 months ago
#4
(Original post by Blaze3211)
Thanks a lot I really appreciate it and have learned a lot from your post. Do you know how I would go about making the input argument the file name which returns the dictionary once called upon. I have tried simply putting the name of the file into the parenthesis but whenever I put something in there I tend to get the invalid syntax error.
Glad that's helped! I'm not sure what you tried, it sounds like you weren't far off. A couple of things:
  • You need to give the def read_file(): function a parameter, which is a variable (name) whose value is provided by the code which calls the read_file() function.

  • Inside the read_file() function body, the name of the parameter is used just like any other variable -- it's needed when you open the file. i.e.:
Code:
def read_file(filename):
  with open(filename) as fin:
Further down where you're actually calling/using the read_file() function, the function is now expecting to be provided with a filename because you've added that parameter. so you'd provide the string that you want to be used with that parameter:
Code:
read_file("seeds2.txt")

(or you can put the "seeds2.txt" string into a variable then provide the variable - that will also work.)
Code:
filename = "seeds2.txt")
read_file(filename)
0
reply
Blaze3211
Badges: 11
Rep:
?
#5
Report Thread starter 7 months ago
#5
(Original post by winterscoming)
Glad that's helped! I'm not sure what you tried, it sounds like you weren't far off. A couple of things:
  • You need to give the def read_file(): function a parameter, which is a variable (name) whose value is provided by the code which calls the read_file() function.

  • Inside the read_file() function body, the name of the parameter is used just like any other variable -- it's needed when you open the file. i.e.:
Code:
def read_file(filename):
  with open(filename) as fin:
Further down where you're actually calling/using the read_file() function, the function is now expecting to be provided with a filename because you've added that parameter. so you'd provide the string that you want to be used with that parameter:
Code:
read_file("seeds2.txt")

(or you can put the "seeds2.txt" string into a variable then provide the variable - that will also work.)
Code:
filename = "seeds2.txt")
read_file(filename)
It seems like I wasn't far off, I just removed the " " and the .txt bit from the initial parenthesis defining the function and it worked well. Thanks a lot again. It seems so simple but you've helped a great deal.
1
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Regarding Ofqual's most recent update, do you think you will be given a fair grade this summer?

Yes (221)
33.74%
No (434)
66.26%

Watched Threads

View All