Reputation: 129
I am writing a script program that opens, reads and splits string data from text file, make a list of it.
donations.txt
SALES Pacal Dennis 20.50 34.70 55.20 12.50
MARKETING Smith James 34.00 31.00 21.00 14.50
SALES Perera Nisal 19.90 17.80 19.20 21.00
...
While going through "r" and .read() in the code, it comes different outcomes. For use of "r" recognize as each text line:
f = open("donations.txt","r")
for line in f:
linelist = line.split()
print(linelist)
Outcome:
['SALES', 'Pacal', 'Dennis', '20.50', '34.70', '55.20', '12.50']
['MARKETING', 'Smith', 'James', '34.00', '31.00', '21.00', '14.50']
['SALES', 'Perera', 'Nisal', '19.90', '17.80', '19.20', '21.00']
for .read() recognize each single character:
f = open("donations.txt").read()
for line in f:
linelist = line.split()
print(linelist)
Outcome:
['S']
['A']
['L']
['E']
['S']
[]
['P']
...
What are those make it differences?
Upvotes: 0
Views: 54
Reputation: 3341
I believe it's pretty obvious there isn't it? A simple web search would have given you all the answers (either python docs or Stackoverflow BTW).
Reading further Python documentation should give you the answers you're looking for. E.g. this links provides many details on various ways to read a file. https://docs.python.org/3/tutorial/inputoutput.html
Only quoting some relevant parts here:
To read a file’s contents, call
f.read(size)
, which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size characters (in text mode) or size bytes (in binary mode) are read and returned. If the end of the file has been reached, f.read() will return an empty string ('').>>> f.read() 'This is the entire file.\n' >>> f.read() ''
f.readline()
reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous; if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by '\n', a string containing only a single newline.>>> f.readline() 'This is the first line of the file.\n' >>> f.readline() 'Second line of the file\n' >>> f.readline() ''
For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:
>>> for line in f: ... print(line, end='') ... This is the first line of the file. Second line of the file
Concerning the file opening, same story : this is well documented in the python docs.
open()
returns a file object, and is most commonly used with two arguments:open(filename, mode)
.
>>> f = open('workfile', 'w')
The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it’s omitted.
Upvotes: 1