Tuesday, May 17, 2016

May 17, 016 (what we did in class)

We discussed techniques to open and read files - the difference between "space" and "tabs" and "commas" - discussed if then and also "while" ...

What else? (!)

In the pffreadcsv program, I check for "Block" in the first "column"

What if that line does not start with "Block" - ?

I can modify the read as follows

# what if Block is NOT in the first column?
# read the file line by line
nstart = 0
for i in lines:
    nstart = nstart + 1
# the split command splits the line into individual elements as it were -
# so a line that is 1,2,1200,500 will be split into 1 2 1200 and 500 using the comma as a separator
    thisline = i.split(',')
# if I want to look for a line that has 'Block' (because I happen to know the data comes after this line)
    nchars = len(thisline)
    for j in range(nchars):
        if (str(thisline[j]) == 'Block'):
            print ("In your file ", datafile, " Block shows up in line ", nstart, ' at position ', j )
            break;
        else:
            continue

So you can "look" for any character you want on that line ... (!) 

(this makes it a bit more general than what I had)

1 comment:

  1. the code as I have is just slightly off ... (anyone notice?) (I will post a correction later!)

    ReplyDelete