user3294540
user3294540

Reputation: 29

Python: How to read data from a file and do math with it

How do I read in a file input and then say if it's not a year then don't use that data? If it is a year (4 digit number) then calculate if it's a leap year by doing simple math.

I'm asking more so how do I do this with a file. I can do the math normally but when the file gets involved I have no idea how files work.

edit Also, how do I do separate functions to check if the input is digits and another function to calculate if it's a leap year or not?

file_name_one = input("First file name: ")
file_stream = open(file_name_one, "r")

for line in file_stream:
    year_str = line[:4]
    year = 0
    leap_year = 0
    div_4 = 0
    div_100 = 0
    div_400 = 0

if year_str.isdigit():   # test if year_str is an integer
    year = int(year_str)
    if year%4 == 0:           # check if the year is a leap year
        div_4 = 1
    if year%100 == 0:
        div_100 = 1
    if year%400 == 0:
        div_400 = 1
    if div_4 == 1 & div_100 == 0:
        print (line[:4], "is a leap year" )
    if div_4 == 1 & div_100 == 0 & div_400 == 1:
        print (line[:4], "is a leap year" )
    div_4 = 0
    div_100 = 0
    div_400 = 0

Upvotes: 1

Views: 8177

Answers (4)

gabe
gabe

Reputation: 2511

If the file is named "foo.txt" and if you are in the same directory of the file, then something like:

file_stream = open("foo.txt",'r')
for line in file_stream:

    # as suggested in the comment, it might be a good idea to print the line, 
    # just so you know what the file looks like
    print line

    # the variable line is a string. depending on the formatting of the file, 
    # something along these lines might work:

    year_str = line[:4]
    if year_str.isdigit():   # test if year_str is an integer
        year = int(year_str)
        if not year%4:           # check if the year is a leap year
           # 
           print "%s is a leap year %s"%year
           ....

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54163

For your updated question:

import re

def check_is_year(year):
    # implement as per other answers
    return True # if `year` is valid, else return False

def check_is_leap(year):
    # implement as you will
    return True # if `year` is a leap year, else False

with open("foo.txt") as f:
    years = [year for year in re.findall("/b/d{4}/b",f.read()) if check_is_year(year)]
    for year in years:
        if check_is_leap(year):
            # do whatever you want.

Upvotes: 0

Kevin
Kevin

Reputation: 2182

If you need to read multiple lines of data, then readlines() is a good function to know. Try this:

f = open("myfile.txt", "r")
lines = f.readlines()
for line in lines:
     print line

Modify the fourth line to check whether your data looks like a year.

Upvotes: 0

ImadOS
ImadOS

Reputation: 425

if i understand you want to read from file , is it ?

will for that ,it's really easy :

with open("filename","r") as file :
    input = file.read()

Upvotes: 1

Related Questions