문제

I open a text file, now each element in the text file is separated by a line break. when I use readlines() It keeps this line break in the list, this is after each element so as it looks like this [zebra\n, ant\n,] i was wondering whether there is a easy function or piece of coding that I could use to just strip all the elements of this line break!

this is the code i have written so far

filelist=file.readlines()
file.close()
return filelist
도움이 되었습니까?

해결책

try:

filelist = [line.rstrip('\n') for line in file]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top