简体   繁体   中英

Python read txt file from url and write it line by line

I have a text file in webserver with a list of ids and want to download it to local computer using python Im using below code

    hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
    url = "http://example.com/ids.txt"
    req = urllib2.Request(url, headers=hdr)
    page = urllib2.urlopen(req)
    content = page.read()
    self.debug(content)
    filename = "ids.txt"
    file_ = open(filename, 'w')
    file_.write(content)
    file_.close()

but using this way i can only get first Id not the rest of it. eg my web file contains

1
2
3
4

and im only geting

1

using above code

Keep in mind this is for Python 3:

from urllib.request import urlopen
file = open("filename","w")
url = urlopen("url")
for line in url:
    file.write(line + '\n')
file.close()

For Python 2:

from urllib2 import urlopen
url = urlopen("url")
file = open("filename", "w")
for line in url:
    file.write(line + '\n')
file.close()

easiest way is to use the Urllib libraries for this.

我自己的代码运行良好,这只是一个 URL 问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM