While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?
-
Hi,
What do you have exactly in that cell ?
-
While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?
I tried converting as follows:
cell = self.ui.widget1.item(r,c).text()
x=int(cell) -
I tried using int(cell), but this does not seems to work. Python does not show any error and does not convert the variable 'cell' into integer. Check type for the variable and still shows unicode.
Actual Scenario:
I have used the following method to populate the variable in QTableWidget and want to have a red colour font for negative numbers:self.ui.table_widget.setItem(1,1, QTableWidgetItem(str("{:.2%}".format(VariableA - VariableB))))
Also, I tried removing 'str' from the item input, while setting the value, but even that does not seem to help:
self.ui.table_widget.setItem(1,1, QTableWidgetItem(("{:.2%}".format(VariableA - VariableB)))) -
I tried using int(cell), but this does not seems to work. Python does not show any error and does not convert the variable 'cell' into integer. Check type for the variable and still shows unicode.
Actual Scenario:
I have used the following method to populate the variable in QTableWidget and want to have a red colour font for negative numbers:self.ui.table_widget.setItem(1,1, QTableWidgetItem(str("{:.2%}".format(VariableA - VariableB))))
Also, I tried removing 'str' from the item input, while setting the value, but even that does not seem to help:
self.ui.table_widget.setItem(1,1, QTableWidgetItem(("{:.2%}".format(VariableA - VariableB))))@Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:
I tried using int(cell), but this does not seems to work.
That's why @SGaist asked what is inside cell?
You can check this with:print(type(cell))
cell probably contains an instance of a Qt class and not just a string.
-
Cell content varies from text or number. I am retrieving it using the following:
cell = self.ui.table_widget.item(row,column).text()
Cell values for ex:
Ex 1: print cell (output: -4.07%)
print type(cell) (output: <type 'unicode'>)Ex 2: print cell (output: Truncated)
print type(cell) (output: <type 'unicode'>) -
@Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:
I tried using int(cell), but this does not seems to work.
That's why @SGaist asked what is inside cell?
You can check this with:print(type(cell))
cell probably contains an instance of a Qt class and not just a string.
The OP stated (assuming that's the case) that the code is:
cell = self.ui.widget1.item(r,c).text()
So from PyQt that is typestr
, and in Python that is Unicode.I tried using int(cell), but this does not seems to work. Python does not show any error and does not convert the variable 'cell' into integer. Check type for the variable and still shows unicode.
What does "does not seems to work" mean? Line:
x=int(cell)
doesn't change/convert variablecell
, it returns the integer value inx
, or raises an error. So after that line what is value ofx
? And what is value (not type) ofcell
? -
Cell content varies from text or number. I am retrieving it using the following:
cell = self.ui.table_widget.item(row,column).text()
Cell values for ex:
Ex 1: print cell (output: -4.07%)
print type(cell) (output: <type 'unicode'>)Ex 2: print cell (output: Truncated)
print type(cell) (output: <type 'unicode'>) -
@Piyush You cannot convert strings like "-4.07%" to a number using int() because it contains % character ans isn't an integer at all. Furthermore int() would convert to integer - do you want integer or floating point numbers?
@jsulm I want a floating point number. Do i need to change the method to retrieve cell value?
What does "does not seems to work" mean? Line:
x=int(cell)
doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?@JonB x=int(cell) does not change/convert variable cell. I tried printing the value of x, but there was nothing in x, which means the conversion is not working. @jsulm is right as % string value cannot be converted into floating point number.
-
@Piyush You cannot convert strings like "-4.07%" to a number using int() because it contains % character ans isn't an integer at all. Furthermore int() would convert to integer - do you want integer or floating point numbers?
@jsulm I want a floating point number. Do i need to change the method to retrieve cell value?
What does "does not seems to work" mean? Line:
x=int(cell)
doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?@JonB x=int(cell) does not change/convert variable cell. I tried printing the value of x, but there was nothing in x, which means the conversion is not working. @jsulm is right as % string value cannot be converted into floating point number.
@Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:
I want a floating point number
Then why do you use int() instead of float()?
Use float:float(cell.strip('%'))
-
@Piyush You cannot convert strings like "-4.07%" to a number using int() because it contains % character ans isn't an integer at all. Furthermore int() would convert to integer - do you want integer or floating point numbers?
@jsulm I want a floating point number. Do i need to change the method to retrieve cell value?
What does "does not seems to work" mean? Line:
x=int(cell)
doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?@JonB x=int(cell) does not change/convert variable cell. I tried printing the value of x, but there was nothing in x, which means the conversion is not working. @jsulm is right as % string value cannot be converted into floating point number.
@Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:
x=int(cell)
doesn't change/convert variable cell, it returns the integer value in x, or raises an error. So after that line what is value of x? And what is value (not type) of cell?@JonB x=int(cell) does not change/convert variable cell. I tried printing the value of x, but there was nothing in x, which means the conversion is not working. @jsulm is right as % string value cannot be converted into floating point number.
Here is Python (3) output for what you are saying is the case:
>>> int("-4.07%") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '-4.07%' >>>
Meanwhile, you say from your code " Python does not show any error" ... ??
-
I tried using int(cell), but this does not seems to work. Python does not show any error and does not convert the variable 'cell' into integer. Check type for the variable and still shows unicode.
Actual Scenario:
I have used the following method to populate the variable in QTableWidget and want to have a red colour font for negative numbers:self.ui.table_widget.setItem(1,1, QTableWidgetItem(str("{:.2%}".format(VariableA - VariableB))))
Also, I tried removing 'str' from the item input, while setting the value, but even that does not seem to help:
self.ui.table_widget.setItem(1,1, QTableWidgetItem(("{:.2%}".format(VariableA - VariableB))))The problem is here:
self.ui.table_widget.setItem(1,1, QTableWidgetItem(str("{:.2%}".format(VariableA - VariableB))))
QTableWidgetItem
's constructor is confusing, see https://bugreports.qt.io/browse/QTBUG-65555use:
newItem = QTableWidgetItem(); newItem-.setData(Qt::EditRole,VariableA - VariableB); self.ui.table_widget.setItem(1,1, newItem);
to format the output as a 2 decimals percentage subclass
QStyledItemDelegate
, reiplemetdisplayText
to use that format and apply it to the column you want withself.ui.table_widget.setItemDelegateForColumn
P.S.
As a general rule, remember that the model should never handle how the data is rendered, that's the job of the delegate -
@Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:
I want a floating point number
Then why do you use int() instead of float()?
Use float:float(cell.strip('%'))
@Piyush said in While retrieving data from QTableWidget, the type appears to be unicode. How can I convert it to number?:
I want a floating point number
Then why do you use int() instead of float()?
Use float:float(cell.strip('%'))
Thanks @jsulm. This solved the issue.