【问题标题】:finding a specific split string in CSV file [Python]在 CSV 文件中查找特定的拆分字符串 [Python]
【发布时间】:2014-04-14 18:38:18
【问题描述】:

我有一个包含人员信息的文件,包括出生日期,并且希望能够调用 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)

【问题讨论】:

  • if DOBsrch == month:????
  • 这是正确的谢谢你

标签: python csv python-3.x opencsv


【解决方案1】:

您需要将intsintsstrsstrs 进行比较。 DOBsrchint,但 DOB 可能是 str(因为您使用其 split 方法)。

所以你需要

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

或者,至少

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

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

if DOBsrch == month: 

【讨论】:

  • 看来 OP 想要的是 if DOBsrch == month:
  • @devull:是的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 2018-12-02
  • 2017-02-21
  • 2013-07-07
  • 1970-01-01
相关资源
最近更新 更多