本文介绍了如何从urllib readlines()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用http的程序,我想从http读取数据:

I have program using http and I want to read data from http:

data = urllib.request.urlopen(someAddress).read()

并通过readlines()方法准备它的返回行之类的行列表文件。

And prepare from it list of lines like returning lines by readlines() method for file.

怎么做?

推荐答案

urlopen()返回一个像文件一样的对象,并支持 .readlines()

urlopen() returns an object that acts just like a file, and supports .readlines():

response = urllib.request.urlopen(someAddress)

lines = response.readlines():

它还支持迭代:

response = urllib.request.urlopen(someAddress)

for line in response:

您已在响应对象上调用 .read();你也可以在那上面打电话给 str.splitlines()

You already called .read() on the response object; you could also just call str.splitlines() on that:

lines = data.splitlines(True)

其中 True 告诉 str.splitlines()以保留行结尾。

where True tells str.splitlines() to preserve line endings.

这篇关于如何从urllib readlines()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 21:29