gpt4 book ai didi

python - 在 CSV 文件中查找特定的分割字符串 [Python]

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:38 25 4
gpt4 key购买 nike

我有一个包含人员信息(包括出生日期)的文件,并且希望能够以 dd/mm/yyyy 格式调用月份,以查找出生月份的人员姓名。到目前为止我有这个:

def DOBSearch():
DOBsrch = int(input("Please enter the birth month: "))
for row in BkRdr:
DOB = row[6]
day,month,year = DOB.split("/")
if DOB == month:
surname = row[0]
firstname = row[1]
print(firstname, " ",surname)
addrsBk.close

但它返回

Please enter the birth month: 02
#(nothing is printed)

最佳答案

您需要将 intsintsstrsstrs 进行比较。DOBsrch 是一个 int,但 DOB 可能是一个 str (因为您使用了它的 split方法)。

所以你需要

day,month,year = map(int, DOB.split("/"))

或者,至少

day,month,year = DOB.split("/")
month = int(month)
<小时/>

此外,正如 @devnull 指出的那样,您可能希望将 monthDOBsrch 进行比较,而不是 DOB:

if DOBsrch == month: 

关于python - 在 CSV 文件中查找特定的分割字符串 [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23067686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com