gpt4 book ai didi

Python 3 网页转行

转载 作者:行者123 更新时间:2023-12-01 05:13:11 25 4
gpt4 key购买 nike

我尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com