给定一个文本文件的 URL,读取文本文件内容的最简单方法是什么? [英] Given a URL to a text file, what is the simplest way to read the contents of the text file?

查看:44
本文介绍了给定一个文本文件的 URL,读取文本文件内容的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,当给定文本文件的 URL 时,访问文本文件内容并在本地逐行打印文件内容而不保存文本的本地副本的最简单方法是什么文件?

In Python, when given the URL for a text file, what is the simplest way to access the contents off the text file and print the contents of the file out locally line-by-line without saving a local copy of the text file?

TargetURL=http://www.myhost.com/SomeFile.txt
#read the file
#print first line
#print second line
#etc

推荐答案

编辑 09/2016:在 Python 3 及更高版本中使用 urllib.request而不是 urllib2

Edit 09/2016: In Python 3 and up use urllib.request instead of urllib2

其实最简单的方法是:

import urllib2  # the lib that handles the url stuff

data = urllib2.urlopen(target_url) # it's a file like object and works just like a file
for line in data: # files are iterable
    print line

你甚至不需要阅读线",正如威尔建议的那样.您甚至可以将其缩短为:*

You don't even need "readlines", as Will suggested. You could even shorten it to: *

import urllib2

for line in urllib2.urlopen(target_url):
    print line

但请记住,在 Python 中,可读性很重要.

But remember in Python, readability matters.

然而,这是最简单的方法,但不是安全的方法,因为在大多数情况下进行网络编程时,您不知道预期的数据量是否会得到尊重.因此,您通常最好读取固定且合理数量的数据,您知道这些数据足以满足您期望的数据,但会防止您的脚本被淹没:

However, this is the simplest way but not the safe way because most of the time with network programming, you don't know if the amount of data to expect will be respected. So you'd generally better read a fixed and reasonable amount of data, something you know to be enough for the data you expect but will prevent your script from been flooded:

import urllib2

data = urllib2.urlopen("http://www.google.com").read(20000) # read only 20 000 chars
data = data.split("\n") # then split it into lines

for line in data:
    print line

<小时>

* Python 3 中的第二个例子:

import urllib.request  # the lib that handles the url stuff

for line in urllib.request.urlopen(target_url):
    print(line.decode('utf-8')) #utf-8 or iso8859-1 or whatever the page encoding scheme is

这篇关于给定一个文本文件的 URL,读取文本文件内容的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆