[Tutor] Hello Python Tutor - help please!

Cecilia Chavana-Bryant cecilia.chavana-bryant at ouce.ox.ac.uk
Wed Aug 22 22:51:12 CEST 2012


Hola Steven,

Many thanks for your reply. I use a Mac computer with OS X 10.6.8 Snow Leopard. I have not been using Python's interactive interpreter. I've been using the terminal and the editor TextWrangler, is using Python shell much different/easier to learn Python with? I do have it installed on my computer but have opened it just a couple of times. The friend that advised me to learn Python and was supposed to help me learn it installed a few things (modules, perhaps?) on my computer, but then he left for Australia (in his defence, it was a very good job offer and he does still help but the 12 hr time difference makes it difficult) so I never really found out what exactly he installed and what they were for. So, I just started using python with the terminal, perhaps it would be better for me to switch to the Python shell?

Something I have been working on is trying to extract some calibration data from 2 different excel files into a csv file (I've been advised this is a good file format to put data into) so I can then use this to post-process around 4 thousand ASCII files. I thought this would be an easy job to start with (what a fool!!) as really all that is involved is multiplying the calibration values against the target values in the ASCII files and then multiply this by 100 to obtain absolute reflectance values. I wrote the below bit of code from bits of coding I found on the web, to be perfectly honest I wrote something like it and then my friend in Australia rewrote it properly. 


#!/usr/bin/env python

import glob  
import xlrd
import sys

def main(fname, sheet_name):
    wb = xlrd.open_workbook(fname)
    sh = wb.sheet_by_name(sheet_name)
    data1 = sh.col_values(0)
    data2 = sh.col_values(1)
    
    return data1, data2

fname = "Cal_File_P17.xlsx"
sheet_name = "RefPanelData"
(data1, data2) = main(fname)

print data1, data2

I do understand this bit of code (although I wouldn't have been able to write it from scratch) but I do not know where the data is being saved to. I have now installed the xlrd module on my computer and will run the code and see what happens, don't worry, my friend in Australia has already told me off for having the temerity of attempting to write code without running it! There are 5 header rows at the beginning of the excel calibration files and one of these is empty and one is a date so I'm not sure what these rows are going to look like when they are extracted, I shall soon find out. Also, I will have to find a way to insert 25 rows before the calibration headers as the ASCII files have 30 lines detailing the instrument condition at the time of measurement that I want to keep. All the actual measurement values that i will have to manipulate start after the row wavelength (I've attached examples of the files i am working with).  

On a different topic, this is the first time I subscribe to a forum and I've just realised that the long-winded signature of my student email account has been included in my posts, for some reason I thought only the main text would go into the post. I'll try to switch to a different email account as the student signature is almost longer than my posts!

Many thanks for your help,
Cecilia Chavana-Bryant
DPhil Candidate - Remote sensing and tropical phenology
Environmental Change Institute
School of Geography and the Environment
University of Oxford
South Parks Road, Oxford, OX1 3QY
Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php
Tel Direct: +44 (0)1865 275861
Fax: +44 (0)1865 275885

________________________________________
From: tutor-bounces+cecilia.chavana-bryant=ouce.ox.ac.uk at python.org [tutor-bounces+cecilia.chavana-bryant=ouce.ox.ac.uk at python.org] on behalf of Steven D'Aprano [steve at pearwood.info]
Sent: 22 August 2012 15:51
To: tutor at python.org
Subject: Re: [Tutor] Hello Python Tutor - help please!

Hello Cecilia,

My replies are below, interleaved with your comments, which are
prefixed with > marks.


On 22/08/12 20:10, Cecilia Chavana-Bryant wrote:

> By the way, the 3 weeks I spent trying to learn C really ended up
> being spent trying to get to grips with using a terminal for the
>first time in my life.

Unfortunately, there will be a certain amount of that, or at least
something quite similar to a terminal. Fortunately, using Python in
the terminal is usually MUCH easier than C, and in my experience
using Python's interactive interpreter is one of the best ways to
learn the language.

What sort of computer are you using? Windows, Linux, Macintosh, or
something different? I think that most of the people here use Linux
or Windows, but we can probably help you one way or the other.


> Since getting back to work, I was advised to try learning Python
>instead of C as it is a much easier first language to learn.

Yes, definitely, but it is still programming. Don't overestimate
the difficulty, if it was hard programmers couldn't learn to do it
*wink*, but on the other hand it's not trivial either.


> I have been trying, but again, to not great success. I started
>following "A Primer on Scientific programming with Python" but I
> kept getting lost and stuck, specially on the exercises.

If you are willing to make a good, honest effort on the exercises
first, we're happy to help you with them. We do like to see your
attempt first, so that we can suggest fixes rather than solve the
problem for you.


> I have also been advised that I should not try to learn
> programming by following guides but by trying to write the
> programmes I need to analyse my data. Although I can understand
> the logic behind this last bit of advise (it gives context and
> direction to the learning process) I have also gotten stuck
>trying this approach as "I do not know how to programme!".

I'm entirely with you there. Having direction in your learning is
a good thing. But until you understand the basic skills you need,
it will be nothing but frustration and pain!

I recommend that, if nothing else, you work through some basic
tutorials so that you at least have some idea of basic language
constructs like:

- strings
- lists
- functions
- ints
- floats
- basic arithmetic
- importing modules
etc.

You could start with the official Python tutorial:

http://docs.python.org/tutorial/index.html

although I find that it is sometimes a bit wordy. (I should
talk...)

If you get stuck, don't hesitate to come back and ask
questions, that's why we're here.


> Thus, I was hoping that some of you can remember how you got
>started

I learned from the book "Learning Python" by Mark Lutz and
David Ascher, and then by writing oodles and oodles of really
bad code which I have long since thrown away :)


[...]
> So, I am now feeling VERY frustrated and have no idea what on
>Earth I am doing! Can anyone please offer guidance in my
>learning process?

I feel your pain! That's how I feel every time I try to understand
monads in Haskell (don't ask!).

How about if you start off with a simple question you would like
to solve using Python? Something relevant to your work. You may
need to explain some of the concepts to us, since we're not
ecologists. (At least, I'm not, I can't speak for others.)

We can then try to guide you to a solution and introduce concepts
as we go.




--
Steven
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list