0

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?)

1 Answers1

0

1) it knows it's a new line because your text file (usually) has new line character(s) in it at the end of each line (not visible unless you set your editor to show all hidden character(s))

2) there are a few different ways to do the same thing

3) the split() returns a list, so each line will be a list of words and your 'document' will be a list of lists.

nge
  • 116
  • 5
  • the answer on 3 is a good answer, thanks for that. 2 isn't really that helpfull, but at least i know it's possible now. i'll search myself. and for question one: I know there are chars in my text file i can't see, but i want to know why the for loop selects the '\n' to loop on, and not stuff like spaces. – Nick Dewitte Jan 23 '16 at 17:27
  • I misunderstood your question on 1). Python behaves that way when you use for to loop over the file object. 2) I meant, the way you are doing it is fine. There are other ways but I don't see any reason for you to change in this specific example – nge Jan 23 '16 at 17:37
  • The accepted answer in this thread should help: http://stackoverflow.com/questions/16922214/reading-a-text-file-and-splitting-it-into-single-words-in-python – nge Jan 23 '16 at 17:42