大佬教程收集整理的这篇文章主要介绍了在 Python 中从 csv 输入和打印值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 Python 新手,希望有人能给我一些关于以下内容的提示-
我有一个名为“应付帐款”的简单 .csv 文件。我希望在 Python 中读取的 .csv' - 我在下面提供了列名和示例行。
商店 ID | 帐户到期日期 |
---|---|
10229 | 01/07/2019 |
87393 | 31/10/2019 |
70708 | 08/11/2021 |
59565 | 24/07/2021 |
67453 | 07/01/2020 |
我希望能够将 Store ID 列中的数字输入到控制台,并让它打印 Account Expiry Date 列中的相应日期。
然后我想将今天的日期与打印的到期日期进行比较,并根据今天的日期是在帐户到期日期之前还是之后打印“okay”或“account has expired”。我是 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")
这可能不是该程序的最佳版本,但它是一个很好的开始,而且一开始可能是最容易理解的。我建议您阅读此代码所提供的所有内容。
以上是大佬教程为你收集整理的在 Python 中从 csv 输入和打印值全部内容,希望文章能够帮你解决在 Python 中从 csv 输入和打印值所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。