On 10/27/05, Mike Haft <[EMAIL PROTECTED]> wrote:
> Hello all,
>         I'm new to python but so far I have to say its a really good language
>
> I've been having some trouble with File IO can anyone help? I've got the
> basics but my problem is that I have many files (one for each year of the
> last 100 years or so) that look like this:
>
> MONTH  RAIN   AVTEMP  RAD  EVAP
> ****************************************
> 1      12.4    12.0    *   10
> 2      13.9    30.0    *   11
> 3
>

> Thats basically all I've been able to do but I need to write script that
> opens a file, reads the relevent data, stores that data and then closes
> the file and moves on to the next file repeating the action until all the
> files have been read. Then it needs to write a file with all the data it
> has collected but in a different format.

Hi Mike,

**forward to list also...

Couple of things you'll need to get familiar with, to make your life
easier, are the os and os.path module.

I assume you've got all your files in one directory.

os.listdir(directory) will return a list of all the files/directories
in that directory. If you've got other directories in there as well,
you can use os.path.isdir() to check if a given path is a directory or
not. As listdir returns the names, and you need the full-path for
simplicity, and also because os.path.isdir() requires it, you can use
os.path.join() to join the directory and filename in a platform
independent way.

So, for example, to find only the files in my python directory

import os
import os.path

dirPath = "c:/python24"

#Either.
filePaths = []
for item in os.listdir(dirPath):
    fullpath = os.join(dirPath, item)
    if not os.path.isdir(fullpath):
        filepaths.append(fullpath)

for item in filePaths:
    parsedataFunc(item)

#Or

names = os.listdir(dirPath)

for item in names:
    fullpath = os.join(dirPath, item)
    if os.path.isdir(fullpath):
        continue
    else:
        parsedataFunc(item)

#Or, if you really want

filePaths = [os.path.join(direc, item) for item in os.listdir(direc)
if not os.path.isdir(os.path.join(direc, item))]

for item in filePaths:
    parseDataFunc(item)


With regards to your data, you seem to be turning it into a string...
what format are you looking to write out? If it's tab/comma delimited,
check out the csv module also.

I'd recommend just ignoring the header lines, so you're only dealing
with the data, assuming that your column headers remain static.

for line in dataFile:
    if line.startswith("MONTH") or line.startswith("**"):
        continue

And that will skip that line.

Hope that helps, good luck handling wx data. I just used to import it
into Excel and spend several hours swearing... I hope Python treats
you better.

And, a question for the group, I've always found that line[:1] is a
complicated way of writing line[0]... is there any time when line[:1]
!= line[0]?

Regards,

Liam Clarke
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to