How can i read a txt file from a website in python

Im learning NLTk and i need to load in a large file and i dont want to save it on my desktop How can i read in a file with python thats hosted on a website?

I tried this code here but it didnt work, i assume that the open with is the rson for it but i need to use open with because i need to save it as a file - myfile in this case.

import nltk

with open('', 'r')as myfile:'\n', 'r')

data2 = data.replace("/", "")

for i, in line in enummerate(data2.split('\n')):
    if i>10:
    print(str(i) + ':\t' + line)

and this is the error:

Traceback (most recent call last):
  File "", line 3, in <module>
    with open('', 'r')as myfile:
FileNotFoundError: [Errno 2] No such file or directory: ''

What can i do to use the file in my script without downloading the whole file?

I changed the code to work with requests

import nltk
import requests

myfile = requests.get('')'\n', 'r')

but now when i run this i get this error:

Traceback (most recent call last):
  File "", line 6, in <module>'\n', 'r')
AttributeError: 'Response' object has no attribute 'read'

Read more here:

Content Attribution

This content was originally published by yappy twan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: