Open Side Menu Go to the Top
Register
** Python Support Thread ** ** Python Support Thread **

02-21-2013 , 04:05 AM
Regexes can make anyone feel like a Perl programmer.
** Python Support Thread ** Quote
02-21-2013 , 04:44 AM
ironically, regexes in python make me feel like a java programmer.
** Python Support Thread ** Quote
02-21-2013 , 04:04 PM
So far I have this
Code:
from bs4 import BeautifulSoup
import urllib2
import csv

url = 'http://www.basketball-reference.com/teams/ATL/2011/gamelog/'
soup = BeautifulSoup(urllib2.urlopen(url).read())


statsTable = soup.find('table', {"id":"stats"})

games = statsTable.findAll('tr')
stats = []
for gamerow in games:
    cells = [i for i in gamerow.findChildren('td')]
    if len(cells) == 0:
        continue
    if len(cells) == "G":
        continue
    
    gamenum = cells[0].string
    home = cells[3].string !="@"
    result = cells[4].string
    
    stats.append([gamenum, home, result])
        
print stats
and that outputs this

Quote:
[[u'1', False, u'MEM'], [u'2', False, u'PHI'], [u'3', True, u'WAS'], [u'4', False, u'CLE'],.........
I should be able to get that imported to a CSV right?
** Python Support Thread ** Quote
02-21-2013 , 04:38 PM
I don't work with Python2 much, but those u'strings' are unicode right? That might turn out to be a problem as it looks like the python2 csv module doesn't support unicode. it looks like you should be able to convert the strings to ASCII or whatever (like a one-line listcomp?) and then just use the csv module, yeah. (full disclosure: I literally just looked at the csv docs for the first time just now but I'm not the type to say "that looks easy!" and be wrong all that often.)
** Python Support Thread ** Quote
02-21-2013 , 06:27 PM
Quote:
Originally Posted by Xhad
I don't work with Python2 much, but those u'strings' are unicode right? That might turn out to be a problem as it looks like the python2 csv module doesn't support unicode. it looks like you should be able to convert the strings to ASCII or whatever (like a one-line listcomp?) and then just use the csv module, yeah. (full disclosure: I literally just looked at the csv docs for the first time just now but I'm not the type to say "that looks easy!" and be wrong all that often.)
Go from unicode -> asciii -> ansi on your host machine? Yikes!

I'd rather write a loop and copy/paste from a print statement or text file.

Python3 is at least UTF-8 up and down, but it still doesn't solve the issues with the host machine, but the examples at the bottom of the page should be helpful in changing the encoding:

http://docs.python.org/2/library/csv.html#examples

Someone suggested I use Pandas after I complained about how terrible Python CSV is to work with: http://pandas.pydata.org/ I didn't get a chance to work with it, but it looks pretty solid from the docs.
** Python Support Thread ** Quote
02-21-2013 , 08:50 PM
So I tried lxml and it prints out nice and pretty.

Code:
from lxml.html import parse
from urllib2 import urlopen

url = 'http://www.basketball-reference.com/teams/ATL/2011/gamelog'
parsed = parse(urlopen(url))
doc = parsed.getroot()
reg_season = tables[0]
rows = reg_season.findall('.//tr')
def _unpack(row, kind='td'):
    elts = row.findall('.//%s' % kind)
    return [val.text_content() for val in elts]

from pandas.io.parsers import TextParser

def parse_stats_data(table):
    rows = table.findall('.//tr')
    header = _unpack(rows[1], kind = 'th')
    data = [_unpack(r) for r in rows [2:]]
    return TextParser(data, names=header).get_chunk()
The only problem is that since there is duplicate column names it screws every thing up. In Pandas I print this
Code:
ATLreg_season['PTS'].mean()
and it returns
PTS 95.817073
PTS 95.817073

which is points allowed for that year. They should be two different values.

How and where do I go into the code and change the column names?
** Python Support Thread ** Quote
02-22-2013 , 01:19 PM
Hey Guys,

I am super new to programming. I've just gone through the code academy course on python and basically completed it albeit with lots of help from their Q&A forums. And I still feel almost completely clueless.

I've read through this thread and 90% of it goes almost completely over my head. Any advice for the next step as far as useful books or online courses? It seems like lots of noobs ITT are working on the MIT open course?
** Python Support Thread ** Quote
02-22-2013 , 01:28 PM
I like these:

http://docs.python.org/2/ (seriously, the official documentation is decent)
http://swaroopch.com/notes/python/
http://www.barnesandnoble.com/listing/2688416442881

I've also heard good things about http://learnpythonthehardway.org . You may hear about Dive Into Python; avoid it. http://forumserver.twoplustwo.com/19...arted-1295106/

(I also would be interested to hear your detailed opinion about the Python segment of Codecademy; I recently blew threw the entirety of Code Year but I already knew more Python than what appeared in that course so that hampers my ability to have an opinion on it)
** Python Support Thread ** Quote
02-22-2013 , 05:53 PM
Quote:
Originally Posted by NLSoldier
Hey Guys,

I am super new to programming. I've just gone through the code academy course on python and basically completed it albeit with lots of help from their Q&A forums. And I still feel almost completely clueless.

I've read through this thread and 90% of it goes almost completely over my head. Any advice for the next step as far as useful books or online courses? It seems like lots of noobs ITT are working on the MIT open course?
Should check out the edx thread and see what people are saying about that class. Not sure if it is still open for registration, but you ought to sign up for it. There are currently 2 super newbs taking the course now. Should be interesting to see the results.
** Python Support Thread ** Quote
02-22-2013 , 11:55 PM
Xhad-thanks. I had actually just noticed someone talking about learn python the hard way. Seems like a good resource.

I thought code academy's course was decent but def not perfect. I guess my issues would be:
1. It seemed to move from topic to topic w/o really building on things already learned so by the time I got to the end id forgotten most of the concepts from the beginning. This wasn't necc the courses fault as it prob took me a few months of sporadic work to get through it but at one point I actually made a 2nd account so I could go back and re-learn the beginning.

2. When I would get stuck I could usually find the answer in the forums to pass the lesson but rarely find a food explanation for *why* that was the answer which ofc is the more important part when trying to learn.

3. A lot of times the answers in the forums would show some way more elegant method than what the author was trying to get us to do. I guess I probably don't know enough about coding to know if this was necc a bad thing but it was def annoying to see some really simple solution and be like wtf that's all I really needed to do?

4. There def seemed to be a fair number of buggy lessons where the lesson either couldn't be passed or couldn't be passed by doing what the author was actually asking for.

5. Even after completing the course I have zero concept of how to use the actual python software. I downloaded it and the "idle" window comes up which seems to be where I'm supposed to type the code but I can't get it to do even the very simplest of commands.

DaveT- thanks ill def check that out
** Python Support Thread ** Quote
02-23-2013 , 12:15 AM
The instructions are a tad weird. This is a step-by-step on windows:

Find IDLE and put a shortcut on your desktop (this doesn't really matter, but whatever, just do it this way for the moment)

Press CTR-n to create a new file. A new window should pop up.

Press CTR-s to save the file. Save it on your desktop, but be sure to name the file like myFile.py. You have to put the .py extension on there.

Type print 'hello, world' or print('hello world') in your new file. Not sure if you are using 2 or 3, but you should know the basic hello world program.

Press F5 to run the program and IDLE should pop up and print 'hello, world'

Close all your windows down and you should see IDLE and myFile on your desktop. Right-click over myFile and click "Open with IDLE" to open and edit the file.

Absolutely pathetic that Code Academy didn't teach you this.
** Python Support Thread ** Quote
02-23-2013 , 01:00 AM
yeah, if you look in the codecademy thread one of my criticisms about it is that it does nothing to teach you how to program anything outside of their sandbox. Like, this is the first chapter of any decent book and it appears never on their site. I mean, I already knew this but how is the newbie to learn?
** Python Support Thread ** Quote
02-23-2013 , 01:09 AM
Quote:
Originally Posted by Xhad
yeah, if you look in the codecademy thread one of my criticisms about it is that it does nothing to teach you how to program anything outside of their sandbox. Like, this is the first chapter of any decent book and it appears never on their site. I mean, I already knew this but how is the newbie to learn?
I recall Day One. I searched high and low and ended up finding a youtube video with Linux instructions.

Here's something I found on youtube that shows the windows version. Just skip ahead to 2:30 to get a visual step-by-step of what I described.

https://www.youtube.com/watch?v=Ae0z2bIdTH4
** Python Support Thread ** Quote
02-23-2013 , 11:34 AM
Quote:
Originally Posted by NLSoldier
Hey Guys,

I am super new to programming. I've just gone through the code academy course on python and basically completed it albeit with lots of help from their Q&A forums. And I still feel almost completely clueless.

I've read through this thread and 90% of it goes almost completely over my head. Any advice for the next step as far as useful books or online courses? It seems like lots of noobs ITT are working on the MIT open course?
The best for me is to investgate/study other peoples code. Isn't there a fair amount of Python open source source code available?
** Python Support Thread ** Quote
02-23-2013 , 01:34 PM
Thanks so much for the detailed instructions!

I will give it a try as soon as I get home and report back.
** Python Support Thread ** Quote
02-23-2013 , 01:43 PM
Quote:
Originally Posted by adios
The best for me is to investgate/study other peoples code. Isn't there a fair amount of Python open source source code available?
https://github.com/saltstack/salt/bl.../cli/caller.py

Just a random page, but you're telling someone who can't even run "hello world" on his machine to look at the above.

fwiw, it's actually a fairly simple program page* if you know how to read Python, but for a newb, it would be wholly intractable, I think.

* I meant that this single page is very easy, but if you were to try to look at the entire program, it is quite complex. Regardless, at a glance that code looks well-written, but even I'm not entirely sure and I've been working with this language for a while now.

That is the problem with open-source code. There is some seriously bad stuff out there, and it so happens that the simple stuff to read in python tends to be very bad, which is exactly the stuff a newb will look at to learn about programming.
** Python Support Thread ** Quote
03-09-2013 , 03:56 PM
Quote:
Originally Posted by daveT
The instructions are a tad weird. This is a step-by-step on windows:

Find IDLE and put a shortcut on your desktop (this doesn't really matter, but whatever, just do it this way for the moment)

Press CTR-n to create a new file. A new window should pop up.

Press CTR-s to save the file. Save it on your desktop, but be sure to name the file like myFile.py. You have to put the .py extension on there.

Type print 'hello, world' or print('hello world') in your new file. Not sure if you are using 2 or 3, but you should know the basic hello world program.

Press F5 to run the program and IDLE should pop up and print 'hello, world'

Close all your windows down and you should see IDLE and myFile on your desktop. Right-click over myFile and click "Open with IDLE" to open and edit the file.

Absolutely pathetic that Code Academy didn't teach you this.
the bolded step is not working for me for some reason(something briefly flashes on the screen but nothing opens)...this is just the same as opening idle and then file--->open and selecting myFile though right?
** Python Support Thread ** Quote
03-09-2013 , 04:40 PM
The best I can figure is you didn't put the .py extension on:



It doesn't default to .py.

Yes, it is also the same as file --> open.
** Python Support Thread ** Quote
03-09-2013 , 04:43 PM
hmm i started doing learnpythonthehardway and he has me using powershell and notepad++ and now i feel like im actually coding and its working well. any issue with just sticking to that for now?
** Python Support Thread ** Quote
03-09-2013 , 05:59 PM
Not at all. In fact, don't revisit this question for at least a year unless you hit some annoyance that specifically makes you want to try something else.
** Python Support Thread ** Quote
03-09-2013 , 06:33 PM
ive been cruising through learnthehardway but have gotten kinda stuck on #16

even when i copy/paste his code I get this error:

PS C:\> python ex16cheat.py test1.txt
We're going to erase 'test1.txt'.
If you don't want that, hit CTRL-C (^C).
If you do want that, hit RETURN.
?
Opening the file...
Traceback (most recent call last):
File "ex16cheat.py", line 12, in <module>
target = open(filename, 'w')
IOError: [Errno 13] Permission denied: 'test1.txt'

seems like its saying i don't have permission to delete the file (that i just created)? any idea what I'm doing wrong here?
** Python Support Thread ** Quote
03-09-2013 , 06:34 PM
Try running the shell in administrator? (Right click... run as administrator)
** Python Support Thread ** Quote
03-09-2013 , 06:35 PM
Btw, you can stick with notepad++ for now / for that course, but later on, Sublime 2 is imo the sickest text editor ever.
** Python Support Thread ** Quote
03-09-2013 , 06:38 PM
Quote:
Originally Posted by Alex Wice
Try running the shell in administrator? (Right click... run as administrator)
This was my first thought...second was that you might have the test file open in another program or windows might think that you do (easy way to test this is to use a different filename)
** Python Support Thread ** Quote
03-09-2013 , 06:43 PM
yep running as admin fixed it. thanks guys
** Python Support Thread ** Quote

      
m