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

标签 python csv python-3.x opencsv

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

相关文章:

python - 清理 csv 文件的 python tweepy 输出

python - 正确关闭使用 skimage.measure.find_contours() 生成的多边形

python - 对 Python 中 pandas 维度的一些规则进行分组

python - 为什么 `bin` 目录在 Windows 上的名称不同 (`Scripts` )?

python - 如果不存在并基于 2 列条件,则在 df pandas 中添加行

python - 如何使用请求发送带有 header 的 PATCH 请求

python - 将整数日期格式转换为人类可读格式

python - 如何使用 MNIST 在 tensorflow 中减少权重位或更改为较低位类型?

csv - 07002 [Microsoft][ODBC SQL Server 驱动程序]COUNT 字段不正确或语法错误

excel - vba打开csv文件困惑的日期格式