Posts: 4,229
Threads: 97
Joined: Sep 2016
split would have cleared out the white space. This includes the return character at the end of the line. Since you didn't use it, you need to use strip before isdigit: line.strip().isdigit(). It's including the last line because there is no return character at the end of the file.
If you are not allowed to use the split() method, getting the years out of the other text is going to be a pain. Can you confirm that?
I guess you could do line[:5].strip() to clear out the other text.
Posts: 13
Threads: 4
Joined: Sep 2017
This is my updated code and it only prints out the last line in the input file which is "6464". Any ideas what I need to change? I can't use a split() function in my assignment.
# function 4
def leapYear(year):
""" Calculates whether a year is a leap year or not. """
year = int(year)
return year % 4 == 0 and (year % 10 != 0 or year % 400 == 0)
# function 3
def writeFile():
""" Opens, writes to, and closes output file. """
file_2 = open(output_file, "w") # open output file
file_2.write(checkRawData()) # write to output file
file_2.close()
# function 2
def checkRawData():
""" Checks raw data by looping through conditions. """
for line in readFile(): # loop through input file
if line[:4].isdigit():
if leapYear(line[:4]):
filtered_data = line[:4]
return filtered_data
# function 1
def readFile():
""" Opens, reads and closes input file. """
try:
file_1 = open(input_file, "r") # open input file
lines = file_1.readlines() # read input file
file_1.close()
except ValueError as e:
print(e)
return lines
# user input
input_file = input("Enter the name of the input file you want to read: ")
output_file = input("Enter the name of the output file to which you want to write: ")
# function calls
readFile()
checkRawData()
writeFile()
Posts: 4,229
Threads: 97
Joined: Sep 2016
In checkRawData, you are updating filtered_data. But that is only ever one year. You just keep changing what year it is. You either need filtered_data to be a list, which you initialize to empty at the start of checkRawData, and append to each time through the loop; or you need to write in the loop in checkRawData, not in a separate function.
Note that your function calls are redundant. At the end of the program you have three calls: - readFile(), but you don't do anything with the return value.
- checkRawData(), which calls readFile() again, and you don't do anything with the returned filtered_data.
- writeFile(), which calls checkRawData() again, which calls readFile() a third time.
At the end of the file you just need one call to the top level function, currently writeFile.
Posts: 13
Threads: 4
Joined: Sep 2017
Sep-20-2017, 12:47 AM
(This post was last modified: Sep-20-2017, 12:48 AM by forumer444.)
Quote:[*]readFile(), but you don't do anything with the return value.
[*]checkRawData(), which calls readFile() again, and you don't do anything with the returned filtered_data.
[*]writeFile(), which calls checkRawData() again, which calls readFile() a third time.
[/list]
At the end of the file you just need one call to the top level function, currently writeFile.
How does this look now? It's my final edit:
def readFile():
""" Reads the input file entered by the user. """
try:
file_1 = open(input_file, "r")
lines = file_1.readlines()
file_1.close()
except FileNotFoundError as e:
print(e)
return lines
def checkRawData():
""" Checks raw data inside the input file. """
filtered_data = ""
for line in readFile():
if line[:4].strip().isdigit():
if leapYear(line[:4]):
filtered_data += line[:4] + "\n"
return filtered_data
def leapYear(year):
""" Calculates whether a year is a leap year or not. """
year = int(year)
return year % 4 == 0 and (year % 10 != 0 or year % 400 == 0)
def writeFile():
""" Writes to the output file entered by the user. """
try:
file_2 = open(output_file, "w")
file_2.write(checkRawData())
file_2.close()
except FileNotFoundError as e:
print(e)
# user input
input_file = input("Enter the name of the input file you want to read: ")
output_file = input("Enter the name of the output file to which you want to write: ")
# function calls
readFile()
checkRawData()
writeFile()
It works by the way, but is there a better way to structure it?
Posts: 4,229
Threads: 97
Joined: Sep 2016
You are still making redundant function calls at the end of the program. As I said, all you need at the end is one call to writeFile, that calls all the other functions.
If you enter a bad file for the input file, you will get an error on return lines , because lines never gets defined if there is an error reading the file. I would define lines at the top of that function as an empty list. Then it won't cause an error, and it will just go through and write an empty file.
|