[英]Inputting and printing values from csv in Python
我是 Python 的新手,希望有人能給我一些關於以下方面的建議-
我有一個簡單的.csv 文件,標題為“到期帳戶”。 我希望在 Python 中讀取的 .csv' - 我在下面提供了列名和示例行。
店鋪編號 | 帳戶到期日 |
---|---|
10229 | 01/07/2019 |
87393 | 2019/10/31 |
70708 | 08/11/2021 |
59565 | 24/07/2021 |
67453 | 07/01/2020 |
我希望能夠將 Store ID 列中的數字輸入到控制台,並讓它打印 Account Expiry Date 列中的相應日期。
然后我想將今天的日期與打印的到期日期進行比較,並根據今天的日期是在帳戶到期日期之前還是之后打印“好的”或“帳戶已過期”。 我是 Python 的新手 - 任何幫助或指示將不勝感激。
這是一個簡單的草稿。
import datetime
# reading simple and small csvs are easy by hand
with open("mycsv.csv", encoding="utf-8") as fp: # open the file
csv_lines = fp.readlines() # read all line
csv_lines = [line.split(",") for line in csv_lines[1:]] # split the lines into 'records'.
# Note, csv_lines[1:] means we skip the header
input_id = input("Enter store id") # Read the input from the user
for (store_id, exp_date) in csv_lines: # we iterate through the data
if store_id == input_id: # if the ids are the same
# get today's date and parse datetime from the string. Then, we get the date from the datetime object.
if datetime.date.today() <= datetime.datetime.strptime(exp_date, "%d/%m/%Y\n").date():
print("okay")
else:
print("account has expired")
break
else:
print("No record found")
這可能不是該程序的最佳版本,但卻是一個很好的開始,而且一開始可能是最容易理解的。 我建議您閱讀此代碼提供的所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.