I have a program where the user can copy an excel table and paste it on the program itself for data analysis. The only value allowed for the table is float. The problem is that there are 4 different ways to write a float, those being:
x = 123456.789
y = 123456,789
a = 123.456,789
b = 123,456.789
and I need the code to interpret all 4 as:
123456.789
What would be the best way to do that, such that it would work on windows, linux and mac?
Here is the full function:
# Copies csv from clipboard to a pandas dataframe
clipboard_dataframe = pd.read_clipboard()
# Get row and column count
paste_rows = len(clipboard_dataframe.index)
paste_columns = len(clipboard_dataframe.columns)
# Create the table to show the values
paste_janela = QTableWidget(paste_rows, paste_columns)
# Define the labels for headers and indexes
paste_janela.setHorizontalHeaderLabels(clipboard_dataframe.columns)
paste_janela.setVerticalHeaderLabels(clipboard_dataframe.index)
# Populate the table with the proper values
for x in range(paste_janela.rowCount()):
for y in range(paste_janela.columnCount()):
# Error handling in case the cell value isn't a float
if not isinstance(clipboard_dataframe.iat[x,y], numbers.Number):
error_message = QMessageBox.critical(self.janela_principal,
"Erro de importação de dados",
"Houe um erro na importação de dados de tabela. \nOs únicos valores aceitos são números reais")
raise ValueError("Valor inválido foi encontrado na tabela. Só é aceito números reais nas tabelas")
table_value = str(clipboard_dataframe.iat[x,y])
table_item = QTableWidgetItem(table_value)
paste_janela.setItem(x, y, table_item)
# Pass the table to the MDI Area, turning it into a subwindow in the process
self.sandbox_mdiarea.addSubWindow(paste_janela)
# Needed to load the window, otherwise it will be hidden as default
paste_janela.show()
localein the stdlib, notably:locale.atoi()