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