I recently started working with Python, and i'm trying things out. I know some basic instructions in Python, and how they work. But most of the time i don't know the exceptions and small details of those instructions.
I'm trying to make an array, and to put a textfile in that array. i use this code:
document = []
with open('inputfile.txt') as f:
for line in f:
document.append(line.strip().split(' '))
print(document)
What this does is placing the inputfile in variable "f", and then, for "line" in "f" in appends that line as a separated array. i know that the ".strip()" gets rid of the "\n", and the ".split(' ')" tears sentences apart in seperate words. My questions are:
1.) Why does python know that the "line" variable indicates a new line? in other words: why does it "do something" for each line, and not eg. for each word? it works with any word, so it's not that line is some kind of special syntax.
2.) Can i change this to something else?
3.) Why is each line added as a new array (thus creating a 2D array)? why isn't all of the processed text crammed into one array? (I know it's better this way, but that's not the point. The point is: Why?)