如何逐行阅读HTML
这个问题在这里已经有了答案:
你可以使用f.readlines()
而不是f.read()
。 该函数返回文件中所有行的列表。
with open("/home/tony/Downloads/page1/test.html", "r") as f:
for line in f.readlines():
print(line)
或者你可以使用list(f)
。
f = open("/home/tony/Downloads/page1/test.html", "r")
f_lines = list(f)
for line in f_lines:
print(line)
来源:https://docs.python.org/3.5/tutorial/inputoutput.html
f.read()
将尝试读取并产生每个字符,直到满足EOF。 你想要的是f.readlines()
方法:
with open("/home/tony/Downloads/page1/test.html", "r") as f:
for line in f.readlines():
print(line) # The newline is included in line
链接地址: http://www.djcxy.com/p/42343.html
上一篇: How to read HTML line by line
下一篇: Reading a file line by line into elements of an array in Python