In my GUI, made with Qt Designer, I have table, 6 columns and 5 rows(headers not count). In first column will be date in format "DD/MM/YY". How I can read and save to some variable those dates, for future use in pdf report? Dates will not be used in any operations, just copy from table and send to function that build pdf report, so they can be str format.
I tried this:
T=[]
for i in range(self.ui.table_Level_N.rowCount()):
T.append(self.ui.table_Level_N.item(i,0))
but got some strange text:
<PyQt5.QtWidgets.QTableWidgetItem object at 0x0000019A24D903A0>
I assumed that it read dates but not in right format.table_Level_N is my table.
You need to get the text from the QTableWidgetItem and append it to the list. Try this:
T = []
for i in range(self.ui.table_Level_N.rowCount()):
item = self.ui.table_Level_N.item(i, 0)
T.append(item.text())
This will give you the text of the cell in the format you want, which you can then use in your PDF report.
Related
I save my DataFrame as csv and try to open it in excel, problem is that excel converts some of my float data to date format. I use excel 2016.
This is how my DataFrame looks like in excel.
Does anyone have an idea how to stop this ?
You have to select the required column and then press CNT + 1 and then select the correct format. As you are saving the file as CSV, you have to repeat this action every time you open the file as CSV don't save such information and by default excel reads everything as generic format. You can find more details here
If you use Excel to open a CSV file it will attempt to interpret each cell. Something that resembles a date will be formatted as a date. Excel has the same behaviour if you type or paste something that resembles a date into a cell formatted as General.
However, if you paste the same data into a cell that has already been formatted other than General it will no longer be re-interpreted.
Format a blank Excel sheet as you expect the data to appear. Open the CSV file in a text editor such as Notepad. Copy the data then paste it into the Excel sheet.
If you aren't sure how the data should appear, for example because you aren't sure about the number of columns, you can format all of the cells as Text. That will suppress interpretation but you can change the formatting afterwards.
Incidentally, I discovered a bug in Excel that relates to this. When you add a new row to the bottom of a table it inherits the formatting of the row above, however Excel does this in the wrong order. To see this, format a table column as Text. In the row below the last row of the table, formatted General, type '1/1/2022'. Excel misinterprets this as 44562. That is because it interpreted 1/1/2022 as a date then changed the formatting to Text to match the row above.
Consequently, when applying the initial formatting you should select at least as many rows as in your CSV file. The easiest way to achieve this is simply to format entire columns.
In your particular case you probably want to pre-format certain columns as Number.
I'm having a csv file as a source dataset. Currently in the table there is a column that I would like to use Python to loop and extract data from string in each cell. For example, in a cell:
"Quantity changed by 10, Price changed by 90."
I would like to use Python and extract "Quantity, Price" and "10, 90" to create new table with those properties and values. Then use PowerBI Visual to create visuals instead of using Python visual. How should I do that? And is that possible at all?
Edit: Due to all the confusions, I'm adding a screenshot of the column I'm working with.
I would like to go through all rows in the Properties column, get data in each cell, then extract them to create a new table. For example, in this case, the new table will be like:
Properties | Value
Unconnected Height | -2800
Area | -39.883
Regarding code, I have nothing yet, just the base Python snippet PowerBI created:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset = pandas.DataFrame(Properties)
# dataset = dataset.drop_duplicates()
# Paste or type your script code here:
So PowerBI is using pandas and matplotlib to get data and plot data. But I only want to get data and output as a new table.
If Python is not possible, is there a way to use Query?
Extracting the data from the column is fairly easy if the data follows the same format:
s = "Quantity changed by 10, Price changed by 90."
l, sep, r = s.partition(',')
if sep:
quantity = int(l.split()[-1])
price = int(r.split()[-1][:-1])
print(quantity, price)
#10 90
The string contained in the cell is partitioned by the comma to separate the quantity and price strings, then each string it processed to extract the value.
The two photos that I've attached below show a dataframe table and a table that was exported out to csv file. I'm wondering if there is any command that can modify the date so that the dates shown on both files would be the same.
On the dataframe: 2017-08-01 -> but after exporting out it becomes 2017/8/1(Instead ->2017/08/01).
Does anyone know how it can be done, or do I can only manually edit the cell format?
[
pandas.DataFrame.to_csv
When you make the call to the to_csv function, you can supply it the parameter date_format='%Y-%m-%d'.
Check out the documentation. One of the parameters that you can pass to_csv is date_format which allows you to control the format of your date like columns. The format is the same as for datetime
df.to_csv(file_path, date_format="%Y-%m-%d")
The form YYYY-MM-DD should be the default output date format for to_csv().
It seems like you are opening the output CSV in a program that may be applying its own style/formatting to the dates. Try opening it in a text editor to confirm.
sometimes, the way others answer(date_format) doesn't work although it is a right way.
you should just change your cell format on Excel in that case.
In that case, follow this way:
right click => Format Cell => Category => Custom => Type: yyyy-mm-dd
I want to show some data in table form. I took QTableWidget for it having multiple columns. One column of it will contain time(hh:mm format).
I also want user to edit any item of table but with corresponding format.
I was able to add data in QTableWidget but i couldn't set text format of time column.
This i want to achieve so that user can edit time only in hh:mm format.
If possible please write your answer code in python.
Since you want the user enter time date, I would suggest to reuse the already existing QDateTimeEdit class in the following way:
dateTime = QDateTimeEdit();
dateTime.setDisplayFormat("hh:mm");
dateTime.setFrame(False);
myTableWidget.setCellWidget(row, column, dateTime);
The user will be able to edit the "time data" this way in your table widget. Moreover, it will be also convenient due to the steps that can be applied.
If you really insist on reinventing this yourself, you can use a QLineEdit with custom validator againt the desired hh::mm format.
dateTime = QLineEdit();
dateTime.setValidator(QRegExpValidator(QRegExp("^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$")));
myTableWidget.setCellWidget(row, column, dateTime);
I'm using Python-Excel xlwt to create a blank Excel spreadsheet for filling out in a spreadsheet. I would like to specify that a certain range of cells should be date formatted. I'm doing something like:
datestyle = xlwt.XFStyle()
datestyle.num_format_str = "YYYY-MM-DD"
ws.write(row, column, "", datestyle)
but that's a bit over-prescriptive. People may be pasting in data, and that means that if the format doesn't match exactly then there will be problems. Spreadsheets are generally good at spotting and understanding dates pasted in in various formats. I want the spreadsheet to be able to do this without the restriction of a specific input format.
I just want to say 'this cell is a date' and not impose a format. Is this doable?
You can't specify that a cell is a date and not impose a format, not with xlwt and not with anything else, including Excel itself. Two reasons:
(1) You can't specify that a cell is any type. It is whatever the user types or pastes in. You can format it as a date but they can type in text.
(2) "date" is not a data type in Excel. All Excel knows about is text, floating point numbers, booleans (TRUE/FALSE), errors (#DIV/0 etc), and "blank" (formatting but no data). A date cell is just a number cell with a date format.
A general answer to "Can I do X with xlwt?" questions: Firstly try doing X with Excel / OpenOffice Calc / Gnumeric. If you can't, then neither can xlwt.
The format you're prescribing defines how the date will be displayed, but it won't affect how excel interprets entered dates. If your format is YYYY-MM-DD, the user can still enter 5/21/2008, and the string will be converted to it's date value (39589) and then displayed in your specified format: "2008-01-21"