0

I am trying to figure out how to get my program to read just the first 5 columns of my text file line by line without reading the text above and between the columns and rows because that doesn't contain the data I need to find the day and year of the lowest recorded temperature.

This is how an example of how my text file looks like:

 05/01  80     2002     7       2018    19   1966   61     2013  

05/02   77     1950     3       1945    22   1936   58     2001  

---------------------------------------------------------------------------


Day   Rec Max  Year   Rec Min    Year   Max   Year   Min    Year      

---------------------------------------------------------------------------
08/01   79     2002     8       1981     28   1900   54     1988   

08/02   79     1989     5       1971     31   1994    60    1998    

This is my code(below) that I have so far. I used print to check if I get just the data I need but when I run the program I am getting the text and the hyphens as well. Its not much but I am still working on figuring it out but I am not to figure out what to do next. Thanks to everyone who is willing to help me out in advance.

I am trying to make changes and add on to my existing code below to find the expected output.

 def main():

    file = open ('filename.txt', 'r')  #open the file
    for num in file:
       numbers = num.split() #splits num 
       if len(numbers)> 5:
           print(numbers[0], numbers[1], numbers[2], numbers[3], 
            numbers[4]) #prints the columns 



 main()

The output I get from my code:

05/01 80 2002 7 2018

05/02 77 1950 3 1945

Day Rec_Max Year Rec_Min Year

08/01 79 2002 8 1981

08/02 79 1989 5 1971

Expected Output (example):

Day: 01/01      
Year: 2008                
Lowest Temperature: 32

This is the I made changes to my code.

    def main():  
             file = open ('filename.txt', 'r')     
             for num in file.read().splitlines():      
                 i = num.split()     
                 if len(i)> 6:     
                    print('Day: {}\nYear: {}\nLowest Temperature: {}\n'.format(i[0], i[4], i[1]))   


    main()

This is an example of how I get the output after making changes to my code. How do I get rid of the text from being read in my output and read just the numbers instead?

Day: 88
Year: 1910
Lowest Temperature: 88

Day: Max
Year: Year
Lowest Temperature: Max

1
  • I don't even see your example in your text file. Is that just the format? Commented Apr 13, 2018 at 14:46

1 Answer 1

2

For your test data, you can simply ignore lines without digits, and use format():

with open('test.txt') as f:
    data = f.read().splitlines()
    data = [i.split() for i in data if any(j.isdigit() for j in i)]
    for i in data:
        print('Day: {}\nYear: {}\nLowest Temperature: {}\n'.format(i[0], i[4], i[1]))

Output:

Day: 05/01
Year: 2018
Lowest Temperature: 80

Day: 05/02
Year: 1945
Lowest Temperature: 77

Day: 08/01
Year: 1981
Lowest Temperature: 79

Day: 08/02
Year: 1971
Lowest Temperature: 79
Sign up to request clarification or add additional context in comments.

1 Comment

Can you split this line into a loop? data = [i.split() for i in data if any(j.isdigit() for j in i)]

Your Answer

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