python regex错误:TypeError:期望的字符串或类字节对象

时间:2017-04-21 13:01:04

标签: python regex

我有以下代码。

import datetime
import re


class Entry:
    task_name = None
    minutes = None
    notes = None
    created_at = None

    def __init__(self, task_name=None, minutes=None,
                 created_at=None, notes=None):
        if task_name:
            self.task_name = task_name
            self.minutes = int(minutes)
            self.created_at = datetime.datetime.strptime(
                                                    created_at,
                                                    "%Y-%m-%d %H:%M:%S.%f")
            self.notes = notes
        else:
            self.get_task_details()

    # get task name, num of minutes and notes if required
    def get_task_details(self):
        self.task_name = input("Enter the name of the task: ")
        self.minutes = self.get_minutes()
        self.notes = self.get_notes()
        self.created_at = datetime.datetime.now()

    # get num of minutes and verify it
    # the user should enter a valid input
    def get_minutes(self):
        while True:
            minutes = input("Enter the number of minutes spent on the task: ")
            if minutes:
                try:
                    minutes = int(minutes)
                    return minutes
                except ValueError:
                    print("Please Enter a valid num of minutes")
            else:
                print("Please enter a valid input. ex 12")

    # print task info
    def get_task_info(self):
        print("You created your task at")
        created_at_formatted = self.created_at.strftime("%m/%d/%Y %H:%M")
        print("{}".format(created_at_formatted))

    # get notes if the user wants to
    # else: leave it with a value of None
    def get_notes(self):
        answer = input("Do you want to write any note on this task ? N/y >>> ")
        if answer.lower() == "y":
            note = input("Enter your note >>> ")
            return note
        else:
            self.notes = None
            print("Alright. No notes are added")

    def __str__(self):
        return """
        Task Name: {}
        Created At: {}
        Minutes Spend: {}
        Notes: {}
        """.format(self.task_name, self.created_at, self.minutes, self.notes)

class Log:
    e = Entry()
    entries = []
    entries.append(e)

    def find_by_pattern(self):
        """find by regex pattern"""
        regex = input("Enter your regex >>> ")
        try:
            regex = re.compile(r'{}'.format(regex), re.X)
        except sre_constants.error:
            print("Invalid regex")
            regex = None
        entries_found = []
        # check if the user entered a regex
        if regex:
            for entry in self.entries:
                if (re.findall(regex, entry.task_name) or
                        re.findall(regex, entry.notes)):
                        entries_found.append(entry)
        else:
            print("Please enter a  valid regex")

        if entries_found:
            if entries_found:
                print("We found the following entries")
                for entry in entries_found:
                    print("{}".format(entry))
        else:
            print("No entries were found based on your pattern")

if __name__ == "__main__":
    l = Log()
    l.find_by_pattern()

当我运行程序并输入正则表达式时:' \ w {4}&#39 ;;它崩溃了 但是,如果我运行程序并输入相同的正则表达式但没有引号:\ w {r},它不会崩溃。 以下是我想要做的更清晰的图片:

enter image description here

2 个答案:

答案 0 :(得分:1)

由于正则表达式而导致崩溃,但由于entry.notesNonefindall需要字符串。

它适用于第一个示例,因为正则表达式匹配entry.task_name,因此跳过条件行88-89的其他部分。
但是,在输入引号时,正则表达式与第一部分(它预期引号)不匹配,后者崩溃为None

您可以通过输入注释来验证这一点,但它不会崩溃,尽管将这些属性初始化为空字符串可能更明智。

PS:请注意,在get_notes中你会返回音符(如果有的话),但在另一种情况下你可能想要返回实际值(或者当函数返回时该值将被删除并且无论如何都会设置为None第27行

这是一个快速/不太固定的版本:

import datetime
import re


class Entry:
    task_name = ""
    minutes = None
    notes = ""
    created_at = None

    def __init__(self, task_name=None, minutes=None,
                 created_at=None, notes=""):
        if task_name:
            self.task_name = task_name
            self.minutes = int(minutes)
            self.created_at = datetime.datetime.strptime(
                                                    created_at,
                                                    "%Y-%m-%d %H:%M:%S.%f")
            self.notes = notes
        else:
            self.get_task_details()

    # get task name, num of minutes and notes if required
    def get_task_details(self):
        self.task_name = input("Enter the name of the task: ")
        self.minutes = self.get_minutes()
        self.notes = self.get_notes()
        self.created_at = datetime.datetime.now()

    # get num of minutes and verify it
    # the user should enter a valid input
    def get_minutes(self):
        while True:
            minutes = input("Enter the number of minutes spent on the task: ")
            if minutes:
                try:
                    minutes = int(minutes)
                    return minutes
                except ValueError:
                    print("Please Enter a valid num of minutes")
            else:
                print("Please enter a valid input. ex 12")

    # print task info
    def get_task_info(self):
        print("You created your task at")
        created_at_formatted = self.created_at.strftime("%m/%d/%Y %H:%M")
        print("{}".format(created_at_formatted))

    # get notes if the user wants to
    # else: leave it with a value of None
    def get_notes(self):
        answer = input("Do you want to write any note on this task ? N/y >>> ")
        if answer.lower() == "y":
            note = input("Enter your note >>> ")
            return note
        else:
            return ""
            print("Alright. No notes are added")

    def __str__(self):
        return """
        Task Name: {}
        Created At: {}
        Minutes Spend: {}
        Notes: {}
        """.format(self.task_name, self.created_at, self.minutes, self.notes)

class Log:
    e = Entry()
    entries = []
    entries.append(e)

    def find_by_pattern(self):
        """find by regex pattern"""
        regex = input("Enter your regex >>> ")
        try:
            regex = re.compile(r'{}'.format(regex), re.X)
        except sre_constants.error:
            print("Invalid regex")
            regex = None
        entries_found = []
        # check if the user entered a regex
        if regex:
            for entry in self.entries:
                if (re.findall(regex, entry.task_name) or
                        re.findall(regex, entry.notes)):
                        entries_found.append(entry)
        else:
            print("Please enter a  valid regex")

        if entries_found:
            if entries_found:
                print("We found the following entries")
                for entry in entries_found:
                    print("{}".format(entry))
        else:
            print("No entries were found based on your pattern")

if __name__ == "__main__":
    l = Log()
    l.find_by_pattern()

答案 1 :(得分:0)

基于错误:

expected string or bytes-like object

您只需要使用str()将后面的部分作为字符串类型的数据进行传输:

if (re.findall(regex, str(entry.task_name)) or
                        re.findall(regex, str(entry.notes))):
                        entries_found.append(entry)

或者您可以尝试在findall函数的第二个参数前面添加b'xxx'。不是变量,它是原始字符串。