How to get the first word of a URL? - Python 3.3

This is the place for queries that don't fit in any of the other categories.

How to get the first word of a URL? - Python 3.3

Postby Blaiiz » Wed Feb 13, 2013 8:00 pm

Hey guys, I have an assignment to complete for my CSC course except I have no idea how to do this part of the assignment.
We're supposed to create a code to get the first of word of a desired line that the user inputs. We can't use "\n" in any way to count the lines (or make a code that outputs the desired line). The function has two parameters - (url, line_number). And we have to specifically use the "readline()" function instead of "readlines()" or any other form... the following code must be used:

"stream = urlopen(url)
line = stream.readline().decode()"

The only hint we were given was that we'd need more than one loop to execute the function. I'm not too sure if we can create multiple functions to help with the problem, but if not, how would you do it with just one defined function? Thanks in advance for the help!


Regards,
Blaiiz
Blaiiz
 
Posts: 2
Joined: Wed Feb 13, 2013 7:51 pm

Re: How to get the first word of a URL? - Python 3.3

Postby micseydel » Wed Feb 13, 2013 8:09 pm

Blaiiz wrote:Hey guys, I have an assignment to complete for my CSC course except I have no idea how to do this part of the assignment.
We're supposed to create a code to get the first of word of a desired line that the user inputs. We can't use "\n" in any way to count the lines (or make a code that outputs the desired line). The function has two parameters - (url, line_number). And we have to specifically use the "readline()" function instead of "readlines()" or any other form... the following code must be used:

"stream = urlopen(url)
line = stream.readline().decode()"

The only hint we were given was that we'd need more than one loop to execute the function. I'm not too sure if we can create multiple functions to help with the problem, but if not, how would you do it with just one defined function? Thanks in advance for the help!


Regards,
Blaiiz

In the future, use code tags rather than quotes (it's especially important for code with indentation, but I find it more aesthetically pleasing in posts in general).

We are happy to help with your homework, but we need to see some effort. You're told that you need multiple loops. What have you tried? Why did you try it, and why do you think it doesn't work? If you can't solve the problem, you should still write as much code toward solving the problem as possible. Are you able to iterate through all the available lines, at the very least?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 940
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How to get the first word of a URL? - Python 3.3

Postby metulburr » Wed Feb 13, 2013 8:21 pm

How to get the first word of a URL? - Python 3.3


your title is misleading, I had to reread your post a few times to understand what you were trying to get. At first I thought you were trying to get a section of the URL

Without the info that micseydel said, all i can say is check the docs
http://docs.python.org/3.0/library/urllib.request.html
http://docs.python.org/3.3/library/readline.html
and then google
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1122
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to get the first word of a URL? - Python 3.3

Postby Blaiiz » Wed Feb 13, 2013 11:54 pm

I was trying to get the line first and then the word, but it didn't really work out. :/

All I've got is
Code: Select all
def find_line(url:str,  line_number:int):
    stream = urlopen("URLHERE")
    line = stream.readline().decode()
    linecount = 1
    lines=str(line)
    for line in stream:
        while linecount != line_number:
           
            linecount = linecount + 1
            lines = lines + str(line)
           
    return lines


this code just accumulates and only returns the first two lines
Blaiiz
 
Posts: 2
Joined: Wed Feb 13, 2013 7:51 pm

Re: How to get the first word of a URL? - Python 3.3

Postby stranac » Thu Feb 14, 2013 12:35 am

Here's how you could get the line(it would be better to iterate the file object directly, but using .readline() is a requirement):
Code: Select all
from urllib.request import urlopen

def get_line(url, line_number):
    stream = urlopen(url)
    for i in range(line_number):
        line = stream.readline()
    return line


Now all that remains is getting the first word...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: How to get the first word of a URL? - Python 3.3

Postby micseydel » Thu Feb 14, 2013 12:40 am

Some of your variable names are misleading, see comments.
Code: Select all
def find_line(url:str,  line_number:int):
    stream = urlopen("URLHERE")
    line = stream.readline().decode()
    linecount = 1
    lines=str(line) # str(line) just returns a copy of itself. calling it "lines" doesn't make sense

    # this will iterate over the lines, not making use of readline()
    # further, the first line has already been read using readline(), making it not appear in the loop (except as the "lines" variable)
    for line in stream:
        while linecount != line_number:
           
            linecount = linecount + 1
            lines = lines + str(line)
           
    return lines

Perhaps you want lines to be a list? Change
Code: Select all
lines=str(line)

to
Code: Select all
lines = [line]

and
Code: Select all
lines = lines + str(line)

to
Code: Select all
lines.append(line)

Also, your line count is going to be a bit off. This should be enough that you can tidy up your code and play with it more before I say anymore.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 940
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests