I accidently used `.readlines()`, but why does it look different than in the last exercise?

If I write the solution with .readlines() instead of .readline(), the output looks like a list, with \n s. It reads:

[‘You do look, my son, in a moved sort,\n’, ‘As if you were dismayd: be cheerful, sir.\n’, ‘Our revels now are ended. These our actors,\n’, ‘As I foretold you, were all spirits and\n’, ‘Are melted into air, into thin air:\n’, ‘And, like the baseless fabric of this vision,\n’, ‘The cloud-cappd towers, the gorgeous palaces,\n’, ‘The solemn temples, the great globe itself,\n’, ‘Ye all which it inherit, shall dissolve\n’, ‘And, like this insubstantial pageant faded,\n’, ‘Leave not a rack behind. We are such stuff\n’, ‘As dreams are made on, and our little life\n’, ‘Is rounded with a sleep. Sir, I am vexd;\n’, ‘Bear with my weakness; my, brain is troubled:\n’, ‘Be not disturbd with my infirmity:\n’, ‘If you be pleased, retire into my cell\n’, “And there repose: a turn or two I’ll walk,\n”, ‘To still my beating mind.\n’]
[“Austin runs this show 'round here.”]

In the previous exercise, I used .readlines(), and there were no square brackets or \n s in the output. Why is this?

2 Likes

Hey,

I noticed this too, looked it up and tried it on the previous exercises (1/13, 2/13).

Here, https://docs.python.org/2/library/stdtypes.html?highlight=.readline#file.readline, it says:

Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). 6 If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. When size is not 0, an empty string is returned only when EOF is encountered immediately.

Here, https://www.w3schools.com/python/ref_file_readlines.asp, it says:
The readlines() method returns a list containing each line in the file as a list item.

If you try 1/13, it also returns a list after you use .readlines(). For 2/13, I’m not too sure why it does return a list, but my guess is because each of the lines did not end with a fullstop or comma, and that the next line in 2 lines after.

If anyone else can shed the light on this, it will be most appreciated!

What exactly is the question? .readline() and .readlines() are two different methods, where .readline() returns the next line, and .readlines() returns all lines.

Hey stetim94,

I think the other learner’s is confused why using .readlines() will output the whole file as a list, and each new line of the file as an item of the list.

e.g.:
[‘You do look, my son, in a moved sort,\n’, ‘As if you were dismayd: be cheerful, sir.\n’, ‘Our revels now are ended. These our actors,\n’, ‘As I foretold you, were all spirits and\n’, ‘Are melted into air, into thin air:\n’, ‘And, like the baseless fabric of this vision,\n’, ‘The cloud-cappd towers, the gorgeous palaces,\n’, ‘The solemn temples, the great globe itself,\n’, ‘Ye all which it inherit, shall dissolve\n’, ‘And, like this insubstantial pageant faded,\n’, ‘Leave not a rack behind. We are such stuff\n’, ‘As dreams are made on, and our little life\n’, ‘Is rounded with a sleep. Sir, I am vexd;\n’, ‘Bear with my weakness; my, brain is troubled:\n’, ‘Be not disturbd with my infirmity:\n’, ‘If you be pleased, retire into my cell\n’, “And there repose: a turn or two I’ll walk,\n”, ‘To still my beating mind.\n’]

Instead of what will be printed, if .read() was used:
You do look, my son, in a moved sort,
As if you were dismayd: be cheerful, sir.
Our revels now are ended. These our actors,
As I foretold you, were all spirits and
Are melted into air, into thin air:
And, like the baseless fabric of this vision,
The cloud-cappd towers, the gorgeous palaces,
The solemn temples, the great globe itself,
Ye all which it inherit, shall dissolve
And, like this insubstantial pageant faded,
Leave not a rack behind. We are such stuff
As dreams are made on, and our little life
Is rounded with a sleep. Sir, I am vexd;
Bear with my weakness; my, brain is troubled:
Be not disturbd with my infirmity:
If you be pleased, retire into my cell
And there repose: a turn or two I’ll walk,
To still my beating mind.

1 Like

that is 8 months ago, unlikely he will come back to this topic. Do you have a question?

i think the confusion is here:

but without code is impossible to verify what exactly he did, .readlines should output a list (unless used in a loop i suppose), i think he use .readline in the previous exercise, easy to confuse these methods, but difficult to determine with absolute certainty.

1 Like

Hey again, nope I don’t have a question. Usually as I learn each part, I just check the forum for insights, sometimes I learn more things from the questions people ask.

I initially didn’t know that .readlines() produced a list until I checked it on Google. I know the guy’s isn’t likely to come back to this topic, but possible clarification to his post may help another curious learner out in the future.

He is likely referring to exercise 2/13, because I did .readlines() on the text file and it didn’t return a list.

1 Like

then it crashed.
or you called something else.
or it was a list.

no other options.

the whole purpose of that method is to return lines <- you can tell from the name already without even reading its documentation
or if you do read the documentation, you can see that it does return a list and nothing else

and, in response to your earlier post, periods and commas do not relate to lines in any way, so that won’t affect what is considered a line. that would be incredibly surprising in a bad way.

['1. How many lines do we write on the daily,\n', '2. Many money, we write many many many\n', '3. How many lines do you write on the daily,\n', '4. Say you say many money, you write many many many\n']

I stand corrected. I went back to check, now I understand the previous learner’s and my mistake.
We wrote this:

... for line in xxx.readlines(): 
print line

Instead of this:

 line =  xxx.readlines()
 print(line)

i covered it:

readlines still produce a list, but because a loop is used you never “see” the list. mystery solved.

1 Like

Yep, the loop made the list “invisible”, mystery solved!

don’t think of it as text, it’s structures of data, and those structures support various operations

the brackets spaces and commas you see when printing a list is not part of the list in any way, that is a text representation that is created by extracting values from a list, invoking their tostring code and adding it to the current string being built until the process is complete at which point you have a completely different data structure containing text, that’s no longer a list, that’s just a bunch of characters.

At hardware level you’ve got 1/0 cells in groups of 8
you might represent a character with such a group, that would give you 256 different values (2 ** 8 combinations)
if you take many such groups in a row that could represent a string

now you’d have a string somewhere in memory at a certain location with a certain length
to refer to it you would use the address to that location, that’s an integer
to make a list you might put such integers one immediately after the other. your list is now just numbers, no braces or anything of the like.

to read that list, you’d start at its beginning location, and read enough bits that you have the size of the addressing integer. then you would visit that location, and read the characters there until you reach the end (and somehow you’d have to represent the length or the end), then you’d go back to the list, read the next integer, visit that location, and so on.
meanwhile you might be storing the results in a new location, maybe you’re building up a string representation, that would be another series of groups of 8 bits that you would be adding onto as you went through the list

then you’d have to tell the print procedure about the location of the string you just made

and printing in this case really only means writing those bits to file because your program does not control the monitor or any other display device. often that file is also open by another program such as your terminal application which would in turn generate a configuration of drawing instructions or maybe a complete image to be moved into the memory of your graphics card and that would do its thing and eventually pass information along to the monitor

2 Likes