You want to use re.match but that starts at the beginning of the string. You could use findall instead:
import re
grp = "Application: Company Name / 184010 - Application Development / 184010 - Contract Express"
rgx = "\w+ *(?!.*-)"
res = re.findall(rgx, grp)
print(res) # ['Contract ', 'Express']
Python demo
If there should also not be a forward slash following, you could add that to a character class together with the hyphen.
Note that to not match the space after the word you could omit the space followed by the asterix * from the pattern.
\w+(?!.*[-/])
Regex demo | Python demo
Answer from The fourth bird on Stack OverflowTo check for string equality, just use the == operator:
>>> my_string = "3 ROOM"
>>> my_string == "3 ROOM"
True
>>> my_string == "3 ROOMS"
False
To check if a string contains a substring, use the in operator:
>>> my_string = "13 ROOMS"
>>> "3 ROOM" in my_string
True
>>> "30 ROOM" in my_string
False
To do a more advanced string comparison, you might want to use regular expressions:
>>> import re
>>> my_string = "3 ROOM"
>>> re.match(r"^\d ROOM$", my_string)
<_sre.SRE_Match object at 0x7f4ac226bb90>
>>> re.match(r"^\d ROOMS$", my_string)
None
If you don't need the Regexp, You can use operator in
for row in input:
if("3 ROOM" in input.iloc[row,2] and "ADJOINED FLAT" in input.iloc[row,7]):
ans.append(input[row])
python - AttributeError: 'str' object has no attribute - Stack Overflow
python - AttributeErrors and missing methods - Stack Overflow
python - AttributeError: 'str' object has no attribute 'str' - Stack Overflow
python - Pydantic: Field with regex param ends with AttributeError: 'str' object has no attribute 'match' - Stack Overflow
Videos
I have some experience with programming in Java, C++, etc. and I am trying to write a simple "To-Do List" program to get used to Python. I'm running into the error: str object has no attribute "completed" when trying to iterate over the list of tasks, check their completion status, and display them.
Here are some relevant pieces of the program:
Constructor for the Task class
def __init__(self, task_name):
self.task_name = task_name
self.completed = False
In the ToDoList class (which holds a list of the task instances created by the user) this is the iteration throwing the error in question:
for idx, task in enumerate(self.tasks, start=1):
status = "Completed" if task.completed else "Incomplete"
print(f"{idx}. {task.task_name} - {status}")
I thought, potentially the problem lies in the fact that the enumerate function is grabbing the string value of the task instance, rather than the object itself, so maybe I can iterate over it the old fashioned way and get around it. So I tried it like this:
counter = 1
for task in self.tasks:
status = "Completed" if task.completed else "Incomplete"
print(f"{counter}. {task.task_name} - {status}")
counter += 1
Yet, it throws the same error. I know there is something I am missing or not understanding correctly here. What is it?
Thanks!
There is something wrong with this function. It shows no errors, but when I try to run it, It says AttributeError: 'str' object has no attribute 'current'. (BTW, i am trying to run it as a flet on spyder)
def leapyears(e):
days_in_month = {1: 31, 3: 31, 4: 30, 5:31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31 }
month = int(EnterMonth_text.value)
year = int(EnterYear_text.value)
if year % 100 == 0:
if year % 400 == 0:
leap_year = True
elif year % 4 == 0:
leap_year = True
else:
leap_year = False
if month == 2 :
if leap_year:
days_in_month[2] = 29
else:
days_in_month[2]= 28
output_textfield.value= days_in_month[month]
page.update()