如何按两个值打印字典行数据
我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论