Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我打开一个文本文件,现在文本文件中的每个元素都由换行符分隔。当我使用readlines()它时,它会在列表中保留这个换行符,这是在每个元素之后,所以看起来像这样[zebra\n, ant\n,]我想知道是否有一个简单的函数或一段代码可以用来删除这个换行符的所有元素!
readlines()
[zebra\n, ant\n,]
这是我到目前为止写的代码
filelist=file.readlines() file.close() return filelist
尝试:
filelist = [line.rstrip('\n') for line in file]