我尝试使用 python 3 从网络上读取内容,然后逐一打印所有行。
到目前为止我看到的最好的方法是使用 urllib.request 并做这样的事情:
import urllib.request
url_target = urllib.request.urlopen("http://stackoverflow.com")
tmp_copy_string = url_target.read().decode("utf8")
file = "file"
for word in tmp_copy_string:
print(word)
我以为这段代码逐字打印 - 它不......
问题不仅在于它不逐字打印,而且逐字符打印......
有没有逐行打印的好方法?
不使用额外的库。
最佳答案
您可以通过\n
拆分它:
import urllib.request
url_target = urllib.request.urlopen("http://stackoverflow.com")
tmp_copy_string = url_target.read().decode("utf8").split('\n') #split string on newline
for line in tmp_copy_string:
print(line)
这将逐行打印代码
关于Python 3 网页转行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23777998/