简体   繁体   中英

read text from url line by line in python

I am trying to read data from url which is text file line by line, but i'm getting whole data instead of line by line. It's working fine for large data but it's not working for small data.

url = request.GET.get('url')

data = []
with closing(requests.get(url, stream=True)) as f:
    data = [list(map(int, line.split('\n'))) for line in f]

The above code gives me right answer for files having large row size but it's not working for small data.

For example:(the text file contains)

1 1 1
2 2 2
3 3 3

and I need a list which looks something like this:

[[1,1,1],[2,2,2],[3,3,3]]

what's wrong in my code ?

You can achieve this with readlines() method

response = urllib.request.urlopen('url')

data = response.readlines()

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