AttributeError: 'tuple' object has no attribute 'translate' - python-3.x

AttributeError: 'tuple' object has no attribute 'translate'
mycursor = mydb.cursor()
mycursor.execute("SELECT content FROM news_tb")
myresult = mycursor.fetchall()
for row in myresult:
row = row.translate(str.maketrans('', '', string.punctuation)).lower()
tokens = word_tokenize(row)
listStopword = set(stopwords.words('indonesian'))
wordsFiltered = []
for t in tokens:
if t not in listStopword:
wordsFiltered.append(t)
print(wordsFiltered)
Traceback (most recent call last): File
"C:/Users/Rahmadyan/PycharmProjects/Skripsi/nltk_download.py", line
17, in
row = row.translate(str.maketrans('', '', string.punctuation)).lower() AttributeError: 'tuple' object has no
attribute 'translate'

Even though it is only returning a single column it still puts the value into a tuple just like it would if multiple values were returned.
Each row in the value is going to be something like ("hello",)
To get the string you'll need to access it like this row[0]

Related

AttributeError: 'builtin_function_or_method' object has no attribute 'data'

I was trying to insert a node in a sorted linked list and while traversing it after insertion it is showing attribute error. It is inserting the node and traversing also but at the end it is throwing the error. Can somebody please explain what's wrong here?
def traversal(head):
curNode = head
while curNode is not None:
print(curNode.data , end = '->')
curNode = curNode.next
def insertNode(head,value):
predNode = None
curNode = head
while curNode is not None and curNode.data < value:
predNode = curNode
curNode = curNode.next
newNode = ListNode(value)
newNode.next = curNode
if curNode == head:
head = newNode
else:
predNode.next = newNode
return head
'builtin_function_or_method' object has no attribute 'x'
usually means you forgot to add () to a function call, for example
>>> 'thing'.upper.replace("H", "") #WRONG
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'replace'
>>> "thing".upper().replace("H", "") #RIGHT
'TING'
although your code seems right, I think whatever's calling traversal and insertNode is trying to pass the result of a function as an argument, but it's instead passing the function(it's missing the ())

I want to assert the row text but getting AttributeError: 'WebElement' object has no attribute 'row'

am trying to assert the row text in webtable but am facing issue,getting some attribute error.help me in this
webtable=driver.find_element_by_class_name("dojoxGridScrollbox")
for row in webtable.find_elements_by_xpath("//*[#id='dojox_grid__TreeView_1']/div/div/div/div[1]/table/tbody/tr"):
print(row.text)
driver.implicitly_wait(5)
#assert webtable.row.text== name
assert driver.row.find(name)
Traceback (most recent call last):
File "C:\Users\rajesn\eclipse\Pyhton\GUI_Automation\GuiAutomationTest.py", line 11, in
obj.peer_node()
File "C:\Users\rajesn\eclipse\Pyhton\GUI_Automation\gui_Module.py", line 83, in peer_node
assert driver.row.find(name)
AttributeError: 'WebDriver' object has no attribute 'row'
Given that xpath is very expressive and can be for specific for selecting nodes in a tree, a loop isn't necessary for getting at the inserted table row.
If table row is inserted in top, you can write the query as
first_tr_xpath = "(//*[#id='dojox_grid__TreeView_1']/div/div/div/div[1]/table/tbody/tr)[1]"
first_tr, = webtable.find_elements_by_xpath(first_tr_xpath)
assert first_tr.text.find(name) != -1
If table row is inserted on end of table, it's more like
last_tr_xpath = "(//*[#id='dojox_grid__TreeView_1']/div/div/div/div[1]/table/tbody/tr)[last()]"
last_tr, = webtable.find_elements_by_xpath(last_tr_xpath)
assert last_tr.text.find(name) != -1

AttributeError: 'Series' object has no attribute 'iterrows'

accounts = pd.read_csv('C:/*******/New_export.txt', sep=",", dtype={'number': object})
accounts.columns = ["Number", "F"]
for i, j in accounts["Number"].iterrows(): #i represents the row(index number), j is the number
if (str(j) == "27*******5"):
print(accounts["F"][i], accounts["Number"][i])
I get the following error:
AttributeError: 'Series' object has no attribute 'iterrows'
I don't quite understand the error since "accounts" is a pandas dataframe. Please assist.
accounts["Number"] is a Series object, not a DataFrame. Either iterate over accounts.iterrows() and take the Number column from each row, or use the Series.iteritems() method.
Iterating over the dataframe:
for i, row in accounts.iterrows():
if str(row['Number']) == "27*******5":
print(row["F"], row["Number"])
or over Series.iteritems():
for i, number in accounts['Number'].iteritems():
if str(number) == "27*******5":
print(accounts["F"][i], number)

TypeError: 'generator' object is not subscriptable in python

I am new to python, trying to learn.
I am using below code to read the excel spread sheet but getting below error.
Can anyone help me with how to solve this? Or what is wrong with the code.
import openpyxl
import os
if not os.path.isfile('C:\Python\Python36\EBC_N_Bhatt_Anilkumar _Team - 06132018.xlsx'):
raise Exception('File does not exist.')
wb = openpyxl.load_workbook('C:\Python\Python36\EBC_N_Bhatt_Anilkumar _Team - 06132018.xlsx')
sheet_ind = 0
sheet_names = wb.get_sheet_names()
sheet = wb.get_sheet_by_name(sheet_names[sheet_ind])
r = sheet.max_row
c = sheet.max_column
start_row = 0
for i in range(start_row, r):
cur_row = list(sheet.rows[i])
print(cur_row)
"C:\Users\KVenkataraja\PycharmProjects\Python
Tutorials\venv\Scripts\python.exe"
"C:/Users/KVenkataraja/PycharmProjects/Python Tutorials/readexel.py"
C:/Users/KVenkataraja/PycharmProjects/Python Tutorials/readexel.py:9:
DeprecationWarning: Call to deprecated function get_sheet_names (Use
wb.sheetnames). sheet_names = wb.get_sheet_names()
C:/Users/KVenkataraja/PycharmProjects/Python Tutorials/readexel.py:10:
DeprecationWarning: Call to deprecated function get_sheet_by_name (Use
wb[sheetname]). sheet = wb.get_sheet_by_name(sheet_names[sheet_ind])
Traceback (most recent call last): File
"C:/Users/KVenkataraja/PycharmProjects/Python Tutorials/readexel.py",
line 18, in
cur_row = list(sheet.rows[i])
TypeError: 'generator' object is not subscriptable
Process finished with exit code 1
sheet.rows probably (as your error message suggests) is a generator of rows, that's why it is not sub-scriptable. You should iterate over the generator sheet.rows.
You can, and should iterate it as in the following code snippet:
for row in sheet.rows:
cur_row = list(row)
print(cur_row)
Or if you want the index too, then use enumerate,
for index, row in enumerate(sheet.rows):
cur_row = list(row)
print(cur_row)
# print(index)

Python3.6 - TypeError: 'int' object is not subscriptable

Trying to do the addition of given integer such as 123435 = (1+2+3 = 6) and (4+3+5 = 12) and checking they are equal or not. I am facing an error in following code. I am using python 3.6
def isEqual(n):
num = int(n)
val = len(str(n))
mid = len(str(val))//2
return sum(num(val[:mid])) == sum(num(val[mid:]))
print(isEqual(132435))
val = len(str(n)) ( is is of type int ) you can't make int[:] only with string it works
def isEqual(n):
val = len(str(n))//2
first_sum = eval(('+').join(str(n)[:val]))
second_sum = eval(('+').join(str(n)[val:]))
return first_sum == second_sum
print(isEqual(132435))
try this
return sum(num(val[:mid])) == sum(num(val[mid:]))
Here val value is 6, It is integer you cant use slicing on integer. sum(num(val[:mid]))
You are trying to slice integer, which results in error.
>>> 10[:5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
>>>

Resources