我有一个包含人员信息(包括出生日期)的文件,并且希望能够以 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)
最佳答案
您需要将 ints
与 ints
或 strs
与 strs
进行比较。
DOBsrch
是一个 int
,但 DOB
可能是一个 str
(因为您使用了它的 split
方法)。
所以你需要
day,month,year = map(int, DOB.split("/"))
或者,至少
day,month,year = DOB.split("/")
month = int(month)
<小时/>
此外,正如 @devnull 指出的那样,您可能希望将 month
与 DOBsrch
进行比较,而不是 DOB
:
if DOBsrch == month:
关于python - 在 CSV 文件中查找特定的分割字符串 [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23067686/