0

Problem

i use the following code to read a file and manipulate it. I reuse the first bit of my code for every function to add the data into two files. however the last function does not add data to my list even though it is exactly the identical code? it does not even print the lines of the file?

data_laag = open('/Users/arkin/programming/TN_STAID000162.txt')
data_hoog = open('/Users/arkin/programming/TX_STAID000162.txt')

def temp_laag(data):

    temp = []
    date = []
    line_num = 0

    for line in data_laag:
        if line_num < 22:
            line_num += 1
        else:
            data = line.split(',')
            temp.append(float(data[3])/10)
            date.append(data[2])

    min_temp = 999
    for i in temp:
        if i < min_temp:
            min_temp = i

    index = temp.index(min_temp)
    print 'De minimum temperatuur ooit gemeten is:' ,min_temp, 'Dit was op de volgende datum',      date[index][:4],date[index][4:6],date[index][6:8]

temp_laag(data_laag)

def temp_hoog(data):   

    temp = []
    datum = []
    line_num = 0

    for line in data_hoog:
        if line_num < 22:
            line_num += 1
        else:
            data = line.split(',')
            temp.append(float(data[3])/10)
            datum.append(data[2])

    max_temp = 0
    for i in temp:
        if i > max_temp:
            max_temp = i

    index = temp.index(max_temp)
    print 'De maximum temperatuur gemeten is', max_temp , 'Dit gebeurd op op de volgende datum', datum[index][:4],datum[index][4:6],datum[index][6:8]

temp_hoog(data_hoog)

def aantal_dagen(data):

    temp = []
    date = []
    line_num = 0

    for line in data:
        print line
        if line_num < 22:
            line_num += 1
        else:
            data = line.split(',')
            temp.append(float(data[3])/10)
            date.append(data[2])

    print temp

aantal_dagen(data_laag)
8
  • 1
    You've already read the file once from data_laag so reading it a second time will return nothing Commented Dec 11, 2014 at 20:14
  • oh correct me if i am wrong but you cannot read data twice? how could i correct this? Commented Dec 11, 2014 at 20:15
  • Has to do with pointers while looking at a file simply put do data_biig = open('/Users/arkin/programming/TX_STAID000162.txt') then call aantal_dagen(data_biig) Commented Dec 11, 2014 at 20:17
  • 2
    re-open and re-read the file is the brute force answer. A cleaner approach is to rework each function to pass in the filename rather than a file object. The the function can take care of opening, reading, and closing each file Commented Dec 11, 2014 at 20:18
  • 1
    use with to open your files, if the file sizes are not excessive you can use .readlines to read into memory or use file.seek(0) to go back to the start each time Commented Dec 11, 2014 at 20:19

1 Answer 1

1

There are a number of options, the simplest is to change these two lines

data_laag = open('/Users/arkin/programming/TN_STAID000162.txt')
data_hoog = open('/Users/arkin/programming/TX_STAID000162.txt')

to this

with open('/Users/arkin/programming/TN_STAID000162.txt') as f:
    data_laag = list(f)
with open('/Users/arkin/programming/TX_STAID000162.txt') as f:
    data_hoog = list(f)

Which results in each file being opened, read into an array, and then closed (something you were missing). That means each file is read once, and the data available for re-use in your original code.

But you are going to need to work on dealing with line-endings etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this helped me understanding the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.