In this part readlines areas that don't understand!

Asked 2 years ago, Updated 2 years ago, 15 views

For example, the contents of the txt file Assuming your name, weight, and height,

f =open("~.txt", "r")
print(f.realines())
f.close

I understand that if you write it like this, it's printed in a list format.

By the way,

f =open("~.txt", "r")
r = f.readlines()
for i in r :
   print(i ,end="")
f.close

What's the reason why these cords are cut off and printed out?

Also, ir < I've never seen this before. If it is not a repetitive sentence like ~in range, what exactly does in r mean?

python

2022-09-20 17:59

1 Answers

The readlines() function reads all sentences from a text file and returns them to the list. The list contains one line of text file contents. For example, return a list stored as ['First Line Content', 'Second Line Content', 'Third Line Content',...]

The r returned by r=f.readlines() in the question is the list returned by storing the contents of the text file one line at a time.

If r is a list in for r:, the four-loop turns to take the elements of the list r one by one and put them in i.

If you look at the example below, r is the list, so the for loop turns and puts the elements of the list into i one by one. So if you look at the result, it's printed as print one by one.

r=['1st line\n', '2nd line\n', '3rd line\n']

for i in r:
    print(i)


2022-09-20 17:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.