I have been trying to do some text manipulation in Python and am running into a lot of issues, mainly due a fundamental misunderstanding of how file manipulation works in Python so I am hoping to clear that up.
So lets say I'm iterating through a text file called "my.txt" and it has the following contents:
3 10 7 8
2 9 8 3
4 1 4 2
The code I'm using to iterate through the file is:
file = open ("my.txt", 'r')
for line in file:
print line`
I copied and pasted the above code from a tutorial. I know what it does but I don't know why it works and it's bothering me. I am trying to understand exactly what the variable "line" represents in the file. Is it a data type(a string?) or something else. My instinct tells me that each line represents a string which could then be manipulated(which is what I want) but I also understand that strings are immutable in Python.
What role is memory playing into all this, if my file is too big to fit into memory will it still work? Will line[3] allow me to access the fourth element in each line? If I only want to work on the second line can I do:
if line == 2:
within the for loop?
It might be worth noting that I am pretty new to Python and am coming from a C\C++ background(not used to immutable strings). I know I squeezed quite a few questions into one but any clarification on the general topic would really be helpful :)
lineis, as you thought, a string. In particular is a "line", i.e. at every iterationlinegets assigned to a whole line in the file. Strings are immutable in python(as in most other languages), but they provide a whole lot of methods to manipulate them(creating new strings). Regarding memory: iterating over a file does not read the whole file into memory. It is read by chunks(avoiding both too high memory consumption and too low performances).enumerate:for index, line in enumerate(file_object)This will assignindex = 0to the first line read, thenindex = 1etc. (the syntaxfor a,b,c,d,e in iterableis called tuple-unpacking which may be worth reading about, since it is used a lot).withstatement when working with files in Python.