Something in your program is trying to call the startswith method of an object, probably because it expects it to be a string. You'll have to pay attention to the traceback to see what it is being called on, and why that is an integer instead of a string. Did you pass along an integer where a string was expected?
python - 'int' object has no attribute 'startswith' - Stack Overflow
python - AttributeError: 'str' object has no attribute 'startsWith' - Stack Overflow
Getting a 'NoneType' object has no attribute 'startswith' AttributeError, and i cant figure it out.
'builtin_function_or_method' object has no attr ibute 'startswith'
Something in your program is trying to call the startswith method of an object, probably because it expects it to be a string. You'll have to pay attention to the traceback to see what it is being called on, and why that is an integer instead of a string. Did you pass along an integer where a string was expected?
startswith only works with strings.
If you need to check if an int starts with a set of numbers, you can convert it to a string, i.e.:
someint = 1234
if str(someint).startswith("123"):
# do somenting
So ive googled and tried to change my code based on the other answers ive found, but i keep getting that error code.
Here is my code:
def genre_find(user_id):
global response_user_id
global response
global genreif
if response_user_id != user_id:
response = requests.get('https://www.fanfiction.net/u/{}'.format(user_id))
response_user_id = user_id
sleep(randint(3, 4))
if response.status_code == requests.codes.ok:
soup = bs(response.text, 'html.parser')
user_info = soup.select("#content_wrapper_inner table table tr td")
my_stories = soup.select("div.mystories")
genre = None
for story in my_stories:
genre = story.find_all("div")[-1].text.split(" - ")[3]
return genre
genreif = genre.startswith('Chap')
return genreifError: (line 17 here)
File "test_two.py", line 74, in <module>
user_write_story_data = get_user_information(user_id)
File "test_two.py", line 42, in get_user_information
genre_find(user_id)
File "test_two.py", line 34, in genre_find
genreif = genre.startswith('Chap')
AttributeError: 'NoneType' object has no attribute 'startswith'Hello, I am trying to create a python program that generates a double sha256 hash with a specific number of zeros by changing a nonce But when I run it it gives me an attribute error
NONCE_LIMIT=1000000000
zeroes=4
def mine (block1):
for nonce in range (NONCE_LIMIT):
base_text=str(block1)+str(nonce)
ab=hashlib.sha256(base_text.encode()).hexdigest()
hash_try=hashlib.sha256(ab.encode()).hexdigest
if hash_try.startswith(0*zeroes):
print(f'found hash with nonce: {nonce}')
return hash_try
return -1
block1=bytes.fromhex('22bdef')
mine(block1)```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!
I've been working on setting up a "simple" Flask app with a cloud-hosted postgres database.
My app's config.py file includes the following code:
SQLALCHEMY_DATABASE_URI = os.environ.get('dbs_url')
if os.environ.get('dbs_url').startswith('postgres') :
SQLALCHEMY_DATABASE_URI = os.environ.get('dbs_url').replace('postgres', 'postgresql')Long story short, this is included because ElephantSQL starts their database URLs with "postgres" while SQLAlchemy expects them to begin with "postgresql".
Generally speaking this has worked just fine.
Now, though, I'm in a situation where I need to edit a local database entry directly (I added a new column to an existing table). As far as I know I need to enter Python's shell and import the database model that represents the offending table, like >>> from [app] import [Class]. Apparently doing that involves pulling from the config.py file somehow, because when I try importing a model I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\colby\coding_temple\assignments\week_5\01\homework\app\
from config import Conf
File "C:\Users\colby\coding_temple\assignments\week_5\01\homework\conf
class Conf:
File "C:\Users\colby\coding_temple\assignments\week_5\01\homework\conf
if os.environ.get('dbs_url').startswith('postgres'):
AttributeError: 'NoneType' object has no attribute 'startswith'Regardless of whether what I'm doing makes any sense (I'm sure it doesn't), I'd like to re-write this in a way that doesn't cause this error.
Edit: I was indeed doing something that didn't make any sense. I didn't need to try this at all, just delete a row from my ElephantSQL database. Not sure why I thought there was something locally
As it turns out, the problem was with version compatibility issues between Pandas and Dask. StringMethods is a part of Pandas' string handling functionality, which is used by Dask DataFrames.
However, I didn't know that this attribute was not available in older versions of Pandas.
I updated both libraries and the error was gone:
pip install --upgrade pandas "dask[complete]"
I had the same issue in my conda environment, and my pandas version was up to date. It turns out the solution was with my dask version: we need to install dask via the conda forge channel instead of the standard channel. When you install dask via the standard channel, it only installs up to version 2022.7.0 (as of today). If you install via the conda forge channel, it will do the more recent versions (e.g. 2023.30.0).
In short, the solution for a conda environment is to run:
conda install dask -c conda-forge