Save Tablewidget table to csv file in Python (PyQT Project) [duplicate] - python

I was trying to work with TableWidgets on Python and I ran into an issue.
I wanted to check whether the table is filled or not (with str of course).
def add_table (self):
self.kala = self.comboBox.currentText()
self.code_kala = self.comboBox.currentIndex()
self.vahed = self.comboBox_2.currentText()
list_e = []
for i in list(range(10)):
#self.tab = self.tableWidget.item(i,0)
if self.tableWidget.item(i,0).text() != '':
#if self.tab.text() !='':
list_e.append(i)
else:
pass
self.ROW = len(list_e)
self.tableWidget.setItem(self.ROW,0,QTableWidgetItem(self.kala))
self.tableWidget.setItem(self.ROW,1,QTableWidgetItem(str(self.code_kala)))
self.tableWidget.setItem(self.ROW,2,QTableWidgetItem(str(self.vahed)))
and I don't know why I keep getting this error:
NoneType' object has no attribute 'text'
Does anyone know how to solve it?
Also, I know this code doesn't have any problem (I got good results with the same code in another project) but as cmd said:
File "D:\**\***\*****\*******\*\*************.py", line 1755, in add_table
if self.tableWidget.item(i,0).text() != '':
AttributeError: 'NoneType' object has no attribute 'text'

It seems that the table widget might contain 'None' values, so you have to expect them to pop up. Instead of doing this:
if self.tableWidget.item(i,0).text() != '':
do that:
thing = self.tableWidget.item(i,0)
if thing is not None and thing.text() != '':
# do stuff with thing

Related

The error that I get is AttributeError: 'NoneType' object has no attribute 'date'

I have been working on a script on Python with pandas but I have no previous experience using the library.
I have been following a tutorial to complete this but this part of my code doesn't seem to be working(maybe outdated syntax) despite the instructions.
The error that I get is AttributeError: 'NoneType' object has no attribute 'date':
def get_search_console_data(webproperty, days=-365):
if webproperty is not None:
query = webproperty.query.range(start='today', days=days).dimension('date', 'query')
r = query.get()
df = pd.DataFrame(r.rows)
return df
print("Web property doesn't exist, please select a valid one from this list")
print(account.webproperties)
df = get_search_console_data(webproperty)
df["date"] = pd.to_datetime(df.date)
df[df["date"] > "2021-10-3"]
last_day_queries = df[df["date"] > "2021-10-3"]["query"]
rest_of_queries = df[df["date"] < "2021-10-3"]["query"]
It seems that webproperty which you are passing to the get_search_console_data() function, is None that makes the function return NoneType (actually nothing).
Check whether webproperty is None or not.
Otherwise the df which is being made in the function is None.

TypeError: 'builtin_function_or_method' object is not subscriptable error in python 3

i have been having this error and cannot find a way to fix it here is my code
(i am using python 3.7)
qustion = pyperclip.paste()
qustion = qustion.replace("×", "*")
qustion = qustion.replace("÷", "/")
list_qustion = qustion.split
op_func = ops[str(list_qustion[1])] # the line where the error occurs
answer = op_func(list_qustion[0], list_qustion[2])
print(answer)
qustion.split is a function not a list of values. What I guess you meant is -
list_qustion = qustion.split()
or possibly with the delimiter -
list_qustion = qustion.split('.')

Modifying XML with ElementTree: 'str' object has no attribute 'text'

The following code gives the error:
line 19, in modifyXML
self.PreFlopCallPower.text = 'NEW VALUE'
AttributeError: 'str' object has no attribute 'text'
The problem seems to be that self.PreFlopCallPower is only a string and not an elementTree object, even though my example is very similar to the example given on the python etree documentation: self.PreFlopCallPower = entry.find('PreFlopCallPower').text. How can I fix this? I can't see what I'm doing different to the update example listed here: https://docs.python.org/2/library/xml.etree.elementtree.html
import xml.etree.ElementTree as xml
class XMLHandler(object):
def __init__(self,StrategyName):
self.readXML(StrategyName)
self.modifyXML()
def readXML(self, StrategyName):
self.Template = StrategyName
self.tree = xml.parse('strategies.xml')
self.root = self.tree.getroot()
for entry in self.root.findall('Strategy'):
if entry.get('name') == StrategyName:
self.PreFlopCallPower = entry.find('PreFlopCallPower').text
def modifyXML (self):
self.PreFlopCallPower.text = 'NEW VALUE' # ---ERROR OCCURS HERE---
self.PreFlopCallPower.set('updated', 'yes')
self.tree.write('output.xml')
X=XMLHandler('Strategy305PP')
In this line, you're storing the text of the node (hence have a str):
self.PreFlopCallPower = entry.find('PreFlopCallPower').text
Drop the .text to store the node itself:
self.PreFlopCallPower = entry.find('PreFlopCallPower')

TypeError: argument of type 'method' is not iterable

Error
Traceback (most recent call last):
File "C:/Users/RCS/Desktop/Project/SHM.py", line 435, in <module>
app = SHM()
File "C:/Users/RCS/Desktop/Project/SHM.py", line 34, in __init__
frame = F(container, self)
File "C:/Users/RCS/Desktop/Project/SHM.py", line 384, in __init__
if "3202" in q:
TypeError: argument of type 'method' is not iterable
code
some part of code, initialisation and all
while 1:
q = variable1.get
if "3202" in q:
variable2.set("NI NODE3202")
try:
switch(labelframe2, labelframe1)
except:
switch(labelframe3, labelframe1)
elif "3212" in q:
variable2.set("NI NODE3212")
try:
switch(labelframe1, labelframe2)
except:
switch(labelframe3, labelframe2)
elif "3214" in q:
variable2.set("NI NODE3214")
try:
switch(labelframe1, labelframe3)
except:
switch(labelframe2, labelframe3)
else:
None
some other part of code
def switch(x, y):
if x.isGridded:
x.isGridded = False
x.grid_forget()
y.isGridded = True
y.grid(row=0, column=0)
else:
return False
I am trying to create a switch between three labelframes which are inside another labelframe, and outside this labelframe are other labelframes that are not changing.
I have read some similar answers but I don't want to use __iter__() in my code. Can anybody provide any other suggestions?
You forgot to call the Entry.get() method:
q = variable1.get()
# ^^ call the method
Because the method object itself doesn't support containment testing directly, Python is instead trying to iterate over the object to see if there are any elements contained in it that match your string.
If you call the method, you get a string value instead. Strings do support containment testing.
The reason you got that error was because you did not add "()" after.get query hence the error to fix this change q = variable1.get to q = variable.get()

AttributeError: 'str' object has no attribute 'readline' while trying to compare two string variables

I'm trying to tally the number of instances of a top level domain occur in a file containing 800K+ top level domain strings that I scraped from URLs. In the code below, when I used "if mstlds in ntld:" the results appeared to be correct but upon inspection "co" and "com", "ca" and "cat" counts are incorrect. But if I use == or "is" I don't get any matches at all but instead an error:
Traceback (most recent call last):
File "checktlds4malware.py", line 111, in
mtlds_line = mtlds.readline()
AttributeError: 'str' object has no attribute 'readline'
tld_file = open(sys.argv[1],'r')
tld_line = tld_file.readline()
while tld_line:
#print(tld_line)
tld_line = tld_line.strip()
columns = tld_line.split()
ntld = columns[0] # get the ICANN TLD
ntld = ntld.lower()
mtlds = open ('malwaretlds.txt', 'r')
mtlds_line = mtlds.readline()
while mtlds_line:
print(mtlds_line)
mtlds_line = mtlds_line.strip()
columns = mtlds_line.split()
mtlds = columns[0]
mtlds = mtlds.lower()
#raw_input()
# I don't get the error when using "in" not ==
# but the comparison is not correct.
if mtlds_line == ntld:
m_count += 1
print 'ntld and mtld match: Malware domain count for ', ntld, m_count
mtlds_line = mtlds.readline()
print 'Final malware domain count for ', ntld, m_count
This is because within your while loop, you are setting mtlds to be a String. Thus, once you attempt to use the readline() method you throw the error (pretty self explanatory). You have to remember that only outside the scope of your interior while loop is mtlds pointing to a file.

Categories

Resources