我是 Python 初學者,我們必須讀取 .csv 資料,然后提取日期范圍資料(用戶輸入)。下面的預期輸出示例。如何遍歷閱讀器并提取日期范圍內的行(來自用戶輸入)?
我想我必須使用 datetime.strptime 將輸入的日期和 .csv 檔案中的日期列轉換為日期物件,但我不確定如何處理 .csv 檔案中的日期。然后我必須顯示該時期的新感染人數、結束日期的感染總數、感染人口的百分比以及地區名稱。未知區域可以被忽略并從輸出中排除。.csv 檔案包含大約 3 個月的資料。
我在想我可以將用戶輸入日期中的行附加到一個空串列中,然后寫入一個 csv 檔案?我應該只使用基礎 python,請不要使用 Pandas解決方案。
我當前的代碼:
import csv
from datetime import datetime
#Ask user to input the name of the file they wish to read
file_name = input("Enter the name of the CSV file:\n")
regional_data = open(file_name)
data_reader = csv.reader(regional_data)
cumulativeregional_data = list(data_reader)
#Print 1st and last date for the user before asking for a date range as input
print(f"The first record is for the {cumulativeregional_data[1][0]}\nThe last record is for the {cumulativeregional_data[-1][0]}")
start_date = input("Enter the start date:\n")
startdate_object = datetime.strptime(start_date, "%d/%m/%Y")
end_date = input("Enter the end date:\n")
enddate_object = datetime.strptime(end_date, "%d/%m/%Y")
我們從中讀取資料的 CSV(腳本中的示例):
date,region,region_id,total_infections, adjusted_total_infections, total_deaths, total_recoveries, current_infections, population, day_no, daily_infections, daily_deaths
01/01/2001, Unknown, U,0,0,0,0,0,0,1,0,0
01/01/2001, East,E,5000,0,20,3800,1180,150000,1,100,7
01/01/2001, North,N,3550,0,25,3150,375,180000,1,80,0
01/01/2001, Central,C,4250,0,38,3200,264,175000,1,120,0
01/01/2001, South,S,5525,0,10,5120,395,185000,1,110,0
01/01/2001, West,W,4150,0,45,3850,255,155000,1,80,0
02/02/2001, Unknown, U,0,0,0,0,0,0,2,0,0
02/02/2001, East,E,5300,0,27,3950,1323,150000,2,300,0
02/02/2001, North,N,3750,0,25,3350,375,180000,2,200,5
02/02/2001, Central,C,4350,0,38,3310,1002,175000,2,100,7
02/02/2001, South,S,5550,0,10,5220,320,185000,2,25,1
02/02/2001, West,W,4500,0,45,4000,455,155000,2,350,0
03/01/2001, Unknown, U,0,0,0,0,0,0,3,0,0
03/01/2001, East,E,5450,0,27,4000,1423,150000,3,150,10
03/01/2001, North,N,3825,0,30,3330,465,180000,3,75,3
03/01/2001, Central,C,4475,0,45,3435,995,175000,3,125,10
03/01/2001, South,S,5705,0,11,5300,394,185000,3,155,0
03/01/2001, West,W,4700,0,45,4200,455,155000,3,200,10
04/01/2001, Unknown, U,0,0,0,0,0,0,4,0,0
04/01/2001, East,E,5520,0,37,4200,1283,150000,4,70,0
04/01/2001, North,N,3910,0,33,3510,367,180000,4,85,0
04/01/2001, Central,C,4710,0,55,3550,1105,175000,4,235,0
04/01/2001, South,S,5710,0,11,5500,199,185000,4,5,0
04/01/2001, West,W,4750,0,55,4350,345,155000,4,50,0
我的預期輸出:
預期產出
uj5u.com熱心網友回復:
你可以用這樣的東西做你想做的事:
import csv
from datetime import datetime
#Ask user to input the name of the file they wish to read
file_name = input("Enter the name of the CSV file:\n")
with open(file_name) as csvfile: # recommended when dealing with files to properly open and close files (context manager)
data_in = list(csv.reader(csvfile))
# tell user appropriate date range (first & last date)
print(f"The first record is for the {data_in[1][0]}\nThe last record is for the {data_in[-1][0]}")
# Ask for a date range as input
start_date = datetime.strptime(input("Enter the start date:\n"), "%d/%m/%Y")
end_date = datetime.strptime(input("Enter the end date:\n"), "%d/%m/%Y")
# filter dates
# returns True if date is in between start and end
def filter_func(date):
dt_date = datetime.strptime(date, "%d/%m/%Y")
return (start_date <= dt_date) and (dt_date <= end_date)
# filter list to include dates, removing the headers from data_in
filtered_list = [item for item in data_in[1:] if filter_func(item[0])]
# print out data
total_period_infections = 0
print("New infections\tTotal infections\tPopulation\tPercentage\tRegion") # table headers
for item in filtered_list:
if item[1] == ' Unknown': # filter out the unknown region
continue
total_period_infections = int(item[7]) # to use for the last print statement
print(f"{item[7]}\t{item[3]}\t{item[8]}\t{round(int(item[3]) / int(item[8]), 3)}\t{item[1]}")
print(f"Total new infections for the period: {total_period_infections}")
該程式過濾掉“未知”區域。但是,對于新感染病例,我不確定如何根據提供的資料計算這些數字。該表將需要為您想要的確切版本進行格式化,但是資料已被過濾以包含用戶輸入的那些日期,然后相應地列印出資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/476077.html
