小编mat*_*mat的帖子

python从Web URL读取文件

我目前正在尝试从网站上读取txt文件.

到目前为止我的脚本是:

webFile = urllib.urlopen(currURL)
Run Code Online (Sandbox Code Playgroud)

这样,我可以使用该文件.但是,当我尝试存储文件(in webFile)时,我只获得了一个指向套接字的链接.我尝试的另一个解决方案是使用read()

webFile = urllib.urlopen(currURL).read()
Run Code Online (Sandbox Code Playgroud)

然而,这似乎删除格式化(\n,\t等等)都被删除.

如果我打开这样的文件:

 webFile = urllib.urlopen(currURL)
Run Code Online (Sandbox Code Playgroud)

我可以逐行阅读:

for line in webFile:
    print line
Run Code Online (Sandbox Code Playgroud)

这将导致:

"this" 
"is" 
"a"
"textfile"
Run Code Online (Sandbox Code Playgroud)

但我得到:

't'
'h'
'i'
...
Run Code Online (Sandbox Code Playgroud)

我希望在我的计算机上获取该文件,但同时保持格式.

python urllib readfile

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

readfile ×1

urllib ×1