I have a python program that will print out from CSV. Code will ask the user to enter start date and end date, according to this range program return the temperature.

Here are the code which ask the user to enter start date and end date ( import csv import datetime import time from datetime import datetime from datetime import timedelta

with open('preiction_system.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)

    for row in reader:
        print(row)


ask=input("Enter start  Date\n")

fromDate = time.strptime(ask, "%Y-%m-%d")
start = time.strptime(time.strftime('%Y-%m-%d'),"%Y-%m-%d")


ask=input("Enter End Date\n")

fromDate = time.strptime(ask, "%Y-%m-%d")
end = time.strptime(time.strftime('%Y-%m-%d'),"%Y-%m-%d")

It return the row from csv according to user input dates.

For example start date is 2019-12-01, end date is 2019-12-05

desire output:

Date          maxtempC  mintempC    tempC
2019/12/01  29.80    25.35          29.80
2019/12/02  29.78    25.34          29.78
2019/12/03  30.61    24.53          30.61
2019/12/04  32.08    24.75          32.08
2019/12/05  30.42    24.74          30.42

1 Answers

0
furas On

Using pandas it can be

import pandas as pd

df = pd.read_csv('preiction_system.csv')

#start = input("Enter start  Date\n")
#end = input("Enter end  Date\n")

start = "2019/12/02"
end = "2019/12/04"

result = df[ (df['Date'] >= start ) & (df['Date'] <= end) ]

print(result)

result.to_csv('output.csv')

With csv it can be longer

import csv

# ask before `for` loop
#start = input("Enter start  Date\n")
#end = input("Enter end  Date\n")

start = "2019/12/02"
end = "2019/12/04"

result = []

with open('preiction_system.csv', 'r') as csvFile:
    reader = csv.reader(file_object)

    header = next(reader) # get header
    print(header)
    result.append(result)

    for row in reader:
        if start<= row[0] <= end:
            print(row)
            result.append(row)

# TODO: write result in file