在 Python 中 Selenium 的 readline() 和 readlines() 有什么不同?


以下是 readline() 和 readlines() 方法之间的区别。

readlines()

  • 该方法将一次读取文件的所有内容。

  • 该方法读取文件的所有内容并将其存储在列表中。

  • 该方法使用 readline() 读取到行尾并返回一个列表。

readline()

  • 该方法将读取文件中的一个行。

  • 如果文件没有以新行结尾,新行字符将留在字符串末尾,最后一行的字符将被忽略。

  • 该方法使用 readline() 读取到行尾并返回一个列表。

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

示例

使用 readline() 的代码实现

#open the file for read operation
fl = open('pythonfile.txt')
# reads line by line
ln = fl.readline()
while ln!= "":
print(ln)
ln = fl.readline()
#close the file
fl.close()

使用 readlines() 的代码实现

#open the file for read operation
fl = open('pythonfile.txt')
# reads line by line and stores them in list
for ln in fl.readlines():
print(ln)
#close the file
fl.close()

更新于: 2020 年 7 月 29 日

8K+ 次观看

开启 职业生涯

完成课程后获得认证

开始
广告