You set conn equal to one of two strings:
if Connection_Type == '1':
conn = 'Telnet()'
elif Connection_Type == '2':
conn = 'SSH2()'
So 'Telnet()'.connect nor 'SSH2()'.connect is going to work.
If you have imported SSH or Telnet from somewhere and presuming they are classes then remove the single quotes and you will create an instance which should work once the classes have a connect method.
if Connection_Type == '1':
conn = Telnet()
elif Connection_Type == '2':
conn = SSH2()
Answer from Padraic Cunningham on Stack OverflowAttributeError: 'str' object has no attribute 'extend' when using custom connect_kwargs and key_filename option
amazon web services - python s3 using boto, says 'attribute error: 'str' object has no attribute 'connection' - Stack Overflow
python - AttributeError: 'str' object has no attribute - Stack Overflow
python - AttributeError: 'str' object has no attribute '_execute_on_connection' - Stack Overflow
Videos
Just replace:
key = Key(mybucket)
with:
mybucket = "foo"
bucketobj = conn.get_bucket(mybucket)
mykey = Key(bucketobj)
Expanding on sth's comment, you can't pass a string, it needs to be a bucket object.
Key expects a bucket object as its first parameter (possibly created by conn.create_bucket()).
It looks like mybucket isn't a bucket, but a string, so the call fails.
SQLAlchemy 2.0 (released 2023-01-26) requires that raw SQL queries be wrapped by sqlalchemy.text.
The general solution for this error message is to pass the query text to sqlalchemy.text()
from sqlalchemy import text
...
query = text("SELECT * FROM some_table WHERE column1 > 1")
However in this case the OP is using pandasql, which expects a string. There does not seem to be a straightforward way to make pandasql compatible with SQLAlchemy >= 2.0, and the package seems to be unmaintained, so the only solutions are to find a fork that has fixed the problem (there are some), fork the project yourself and fix it, or downgrade your SQLAlchemy installation using your Python package manager. For example, if you use pip:
python3 -m pip install --upgrade 'sqlalchemy<2.0'
I had same problem with ciavam, and following the comments from snakecharmerb, the problem was solved after wrapped the query string with text():
import pandas as pd
import sqlalchemy as sa
connection = engine.connect()
result = connection.execute("select * from myTables;")
This code returned an error:
AttributeError: 'str' object has no attribute '_execute_on_connection'
So, I wrapped the query as:
connection = engine.connect()
query = sa.text("select * from myTables;")
result = connection.execute(query)
With this correction, I was able to use the result as a dataframe:
my_table = pd.read_sql(query, con=engine)
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()
import socket
class data_pembalap :
def __init__(self,post,no,nama,origin,team,besttime="",tottime=""):
self.pos = post
self.no = no
self.nama = nama
self.origin = origin
self.team = team
self.best_time = besttime
self.total_time = tottime
pembalap = []
def search(no):
for x in range(len(pembalap)):
if pembalap[x].no == no :
return x
return -1
def insert_info(no ,nama ,origin ,team):
x = search(no)
if x >= 0:
pembalap[x].nama = nama
pembalap[x].origin = origin
pemabalap[x].team = team
else:
pembalap.append(data_pembalap(no,nama,origin,team))
def insert_pos(no,pos,total_time):
x = search(no)
if x>=0:
pembalap[x].pos = pos
pembalap[x].total_time = total_time
else:
pass
#pembalap.append( data_pembalap( pos ,no ,total_time))
def insert_time(no,btime ,totime):
x = search (no)
if x>=0:
pembalap[x].best_time = btime
pembalap[x].total_time = totime
else:
pass
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("192.168.100.33" , 50000))
while True:
orbit_data = s.recv(1024)
if not orbit_data:
break
pembalap = orbit_data.decode("utf-8").replace('\"', ' ').split('\r\n')
for x in pembalap:
x = x.split(',')
if x[0] == '$COMP' and len (x) > 7:
no = (x[1])
nama = x[4] + ' ' + x[5]
origin = (x[6])
team = (x[7])
insert_info(no,nama,origin,team)
if x[0] == '$G' and len(x) > 4:
no = (x[2])
pos = (x[1])
total_time=(x[3])
insert_pos(no,pos,total_time)
if x[0] == '$B' and len (x) > 1:
on_track = x[2]
print (on_track)
if x[0] == '$J' and len(x) > 2:
no = x[1]
bs = x[2].replace( '00:','' )
lt = x[3]
insert_time(no,bs,lt)
x = search(no)
all = pembalap.pos ,pembalap.no ,pembalap.nama ,pembalap.origin ,pembalap.btime,
print(all)
****************************************************
Traceback (most recent call last):
File "D:/noname/venv/kon.py", line 69, in <module>
insert_pos(no,pos,total_time)
File "D:/noname/venv/kon.py", line 32, in insert_pos
x = search(no)
File "D:/noname/venv/kon.py", line 17, in search
if pembalap[x].no == no :
AttributeError: 'str' object has no attribute 'no'
Hello ipenk79, I'm a bot that can assist you with code-formatting for reddit. I have detected the following potential issue(s) with your submission:
Python code found in submission text but not encapsulated in a code block.
If I am correct then please follow these instructions to fix your code formatting. Thanks!
pembalap[x] is a string.
Your code is impossible to read as you didn't format it properly, but luckily the error text is stating the cause in plain English.
Go to where you create the list and figure out why it's a string instead of an object.
I'm getting the error which I wrote on the title. This is the code:
town = requests.get("http://earthmc-api.herokuapp.com/towns/" + town)
content = town.content
print(str(content).text)
Error:
AttributeError: 'str' object has no attribute 'text'
How can I fix it?
The result for print(type(town.content)) is:
<class 'bytes'>
If I try to do town.content.text, then it says AttributeError: 'bytes' object has no attribute 'text'
That's the town.content as string:
b'"That town does not exist!"'
But it should be like:
"That town does not exist!"
That's why I'm using .text, and it throws me the error.