Python中逐行读取文件的几种方法
假设在Python脚本的同一个目录下有个文本文件”data.txt”,文件内容为:
She walks in beauty
like the night
Of cloudless climes and starry skies
And all that’s best of dark and bright
Meet in her aspect and her eyes
1.使用readlines()逐行读取文件(针对小文件)
readlines()可以一次性的将文件的所有内容读入内存,然后将文件内容按行分割,返回文件中所有行的list;每行的末尾都会包含一个换行符。
当然这种方式[……]
Read More