2015-10-06 115 views
2

我正在嘗試從網站讀取txt文件。python從網址中讀取文件

我的劇本至今是:

​​

這樣一來,我可以對文件的工作。但是,當我嘗試存儲該文件時(在webFile中),我只能獲取到該套接字的鏈接。我嘗試另一種解決方案是使用read()

webFile = urllib.urlopen(currURL).read() 

然而,這似乎刪除格式化(\n\t等)將被刪除。

如果我打開這樣的文件:

for line in webFile: 
    print line 

這應導致:

"this" 
"is" 
"a" 
"textfile" 

,但我得到

webFile = urllib.urlopen(currURL) 

我可以逐行閱讀:

't' 
'h' 
'i' 
... 

我希望在我的電腦上獲取該文件,但同時保持該格式。

+1

http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python。只需要webFile並將其寫入文件。 – postelrich

+0

有沒有辦法做到這一點,而不是先寫它到本地文件? – mat

回答

4

您應該使用readlines方法()讀取整行:

response = urllib.urlopen(currURL) 
lines = response.readlines() 
for line in lines: 
    . 
    . 

但是,我強烈建議你使用requests庫。 這裏的鏈接

+0

readline爲我做了訣竅,ty – mat

0

這是因爲你迭代了一個字符串。這將導致字符打印的字符。

爲什麼不一次保存整個文件?

import urllib 
webf = urllib.urlopen('http://stackoverflow.com/questions/32971752/python-read-file-from-web-site-url') 
txt = webf.read() 

f = open('destination.txt', 'w+') 
f.write(txt) 
f.close() 

如果你真的想遍歷文件中的行線路使用txt = webf.readlines()和迭代這一點。

0

如果您只是試圖將遠程文件保存爲本地服務器作爲python腳本的一部分,則可以使用PycURL庫下載並保存,而不必解析它。這裏更多的信息 - http://pycurl.sourceforge.net


另外,如果你想讀,然後寫輸出,我覺得你剛剛走出序列的方法。請嘗試以下操作:

# Assign the open file to a variable 
webFile = urllib.urlopen(currURL) 

# Read the file contents to a variable 
file_contents = webFile.read() 
print(file_contents) 

> This will be the file contents 

# Then write to a new local file 
f = open('local file.txt', 'w') 
f.write(file_contents) 

如果兩者都不適用,請更新問題以進行說明。