如何按两个值打印字典行数据

发布于 2025-01-16 07:42:42 字数 1456 浏览 4 评论 0原文

我对 python 很陌生,我有一个关于字典的问题。 我想检查字典数据行之一中是否存在两个值“日”和“月”。 如果是,我想在字典中打印所需的行数据。 我只成功了一个值“日”或“月”,但没有同时成功。知道怎么做吗?

#birthday.csv file:
name,email,year,month,day
Test1,[email protected],1961,03,25
Test2,[email protected],1977,03,21

#python code:
import pandas
import datetime as dt

now = dt.datetime.now()
day = int(now.strftime("%d"))
month = int(now.strftime("%m"))

with open("birthdays.csv") as read_file:
    read = pandas.read_csv(read_file)
    dictionary = read.to_dict('list')
    print(dictionary)
output:
{'name': ['Test1', 'Test2'], 'email': ['[email protected]', '[email protected]'], 'year': [1961, 1977], 'month': [3, 3], 'day': [25, 21]}

我尝试过:

if str(month) in str(dictionary['month']) and str(day) in str(dictionary['day']):
    

我不知道如何以这种方式打印数据行,例如“日”和“月”在行中:Test2,[email protected],1977,03,21 它将打印这一行。

I'm pretty new in python and i have a question regarding dictionary.
i would like to check if two values "day" and "month" are exist in one of the dictionary data rows.
if they are, i would like to print the desired row data in the dictionary.
i only succeeded one value "day" or "month" but not both. any idea how to do it?

#birthday.csv file:
name,email,year,month,day
Test1,[email protected],1961,03,25
Test2,[email protected],1977,03,21

#python code:
import pandas
import datetime as dt

now = dt.datetime.now()
day = int(now.strftime("%d"))
month = int(now.strftime("%m"))

with open("birthdays.csv") as read_file:
    read = pandas.read_csv(read_file)
    dictionary = read.to_dict('list')
    print(dictionary)
output:
{'name': ['Test1', 'Test2'], 'email': ['[email protected]', '[email protected]'], 'year': [1961, 1977], 'month': [3, 3], 'day': [25, 21]}

i tried:

if str(month) in str(dictionary['month']) and str(day) in str(dictionary['day']):
    

i do not know how to print the data row in this way that if for example "day and "month" are in the row:Test2,[email protected],1977,03,21 it will print this row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文