在Python中从csv输入和打印值



我是Python的新手,希望有人能给我一些关于以下内容的建议-

我有一个简单的.csv文件,名为"应付账款"。csv’,我希望在Python中阅读它——我已经提供了下面的列名和示例行。

店铺ID 账户到期日
10229 2019年7月1日
87393 2019年10月31日
70708 2021年11月8日
59565 2021年7月24日
67453 2020年1月7日

这里有一个简单的草稿。

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/%Yn").date(): 
print("okay")
else:
print("account has expired")
break
else:
print("No record found")

这可能不是这个程序最好的版本,但这是一个很好的开始,也许一开始是最容易理解的。我建议您阅读此代码提供的所有内容。

  • Python中的日期处理
  • Python中的文件处理
  • 列出理解
  • Python中的控制流

最新更新