I'm having simple excel files with various timstamp formats written as strings.
There is a build in feature in openpyxl to automatically convert what seems like a date to a datetime object.
My question is simple, how can I take the raw string as it was inserted to the excel file by the user, without intervention of openpyxl.
I want to do my own format testings using a function that tries various calls to datetime.strptime myself.
Loading the excel is done by me like this:
import openpyxl
ex = openpyxl.load_workbook('/path/to/file.xls')
worksheet = ex.active
In case needing to iterate over rows I'm using worksheet.iter_rows method
You're assumption is incorrect: openpyxl does not convert strings unless you ask it to by setting guess_types=True. Excel treats the values as datetime objects by setting the number format and internally converting them to serials with an epoch of 1899-12-30.
Related
I have one Python file that helps me pull and organize datetime data from a server into a pandas DataFrame, and then export out into a nice and usable CSV. My second Python script needs to read the CSV and analyze the data. If I do not touch the exported CSV, the analysis can read the CSV and runs smoothly. However, as soon as I try to merge a few CSV files together with Excel/other spreadsheet software, I get a datetime error
ValueError("time data %r does not match format %r" %
ValueError: time data '2019-12-26 23:00' does not match format '%Y-%m-%d %H:%M:%S'
Even though, it is a direct copy/paste and still saved as a CSV. Any guru can provide some insight on this matter?
Pretty sure this is an Excel issue, not a Python problem.
If you load a .csv that has timestamp strings into Excel, Excel recognizes the datetimes - and formats them. This format seems to default to MM.DD.YYYY hh:mm (the date component might be different depending on your locale):
If you save the file in Excel, the seconds are removed in the .csv!
The only procedure that seems to reliably prevent this behavior is to set a specific date/time format for the respective column, e.g. DD.MM.YYYY hh:mm:ss. AFAIK, You'll have to do this manually for each workbook
Or perhaps write a macro. In older Excel versions, I had a PERSONAL.XLSB for that; should still work with newer versions, you'll have to put it in C:\Users\[username]\AppData\Roaming\Microsoft\Excel\XLSTART
you can use .xlsx format instead of .csv, presumably you won't loose format information there
I see from the error that not all your CSV files have the same time format.
Some on them are in [hour:minute] and some in [hour:minute:second].
ValueError: time data '2019-12-26 23:00' does not match format '%Y-%m-%d %H:%M:%S'
Make sure that datetime fields in all of your CSV matched the same format.
Pandas DataFrames - how do I export list 'X' to a CSV so it appears as a string? The problem is when I open the CSV using Excel it appears in date format.
X=['1-4', '1-5', '2-3', '4-8']
ie. when list 'X' is exported to a CSV and opened with Excel it appears as a date:
I would like list 'X' to appear in Excel as is - that is, not converted it to date format.
Desired output for Excel is:
I have tried the following code - but it throws an error:
import pandas as pd
X=['1-4', '1-5', '2-3', '4-8']
Y=[1,4,3,5]
df=pd.DataFrame(list(zip(X,Y)))
column_names=['A','B']
df.columns=[column_names]
df.A.to_string()
df.to_csv('yyy.csv', mode='a', header=True)
Thankyou
worked fine with me...
maybe the excel or whatever program u use to open the file is casting it... try open it as text file...
Even if Excel reads in date format, when you open in pandas it will come in original format (at least in my case). If someone only wants to save data in csv and work in pandas again, it should be fine.
I also tried doing the 2nd option here (https://www.winhelponline.com/blog/stop-excel-convert-text-to-number-date-format-csv-file/) which transform the data as text. And then saving again. It worked for me.
experts!
I've got two similar Excel files (xlsm) as templates. Both have sheets with Date column.
Visible format for both files when use Excel is "10-Aug-20".
But when I read these files with SSIS process with Script Component Source using Microsoft.ACE.OLEDB.12.0 with "IMEX=1"... ta-da... some I see as expected, but some are 10.08.2020 00:00:00
This causes me a lot of pain because I will process files from both US (MM/dd/yyyy) and German (dd.MM.yyyy) date formats and would like to have locale-independent date format to process dates same way.
How can I force excel to give or ssis to read a correct date format.
Any suggestion how to see both files same programmaticly is most wanted and highly appreciated!
You could try to use the script component (as Transformation) to transform the data by using DateTime.TryParseExact
string dateString = "10-Aug-20";
string format = "dd-MMM-yy";
DateTime dateTime;
if (DateTime.TryParseExact(dateString, format, "en-US",DateTimeStyles.None, out dateTime))//if (DateTime.TryParseExact(dateString, format, "de-DE",DateTimeStyles.None, out dateTime))
{
Console.WriteLine(dateTime);
}
I am working on Protege 5.0 with the plugin Celfie. I have imported an excel sheet which has a column Start_Time (datatype dateTime). When i import the excel sheet into protege, it converts the rows from dateTime format to a string or some other data type. I want to use that column for a data property having the data type DateTime.
I have attached screenshots of both my excel sheet and the celfie tab. Any help would be appreciated.
Celfie Plugin Tab snapshot
Excel Sheet snapshot
The issue is actually a formatting issue in Protege. In Excel, date times are stored as floats, with the integer representing the date and the decimals representing the time.
39448 is 01/01/2008 (https://support.office.com/en-us/article/DATEVALUE-function-df8b07d4-7761-4a93-bc33-b7471bbff252); since the results you're getting in Protege are similar, those seem to be the raw date time values.
You need to convert them in Protege, either by using an informat when you pull them from Excel, or by formatting them in Protege in the same manner that Excel would.
Following the examples provided in the documentation (in particular example 07reader.php) I have managed to extract all the data I needed from an excel file.
However the files I need to import have date values that need to be converted to Y-m-d, so I was wondering if PHPExcel can always output dates in a certain format.
Does anyone know hot to do it? I've looked in the official forum, but I haven't found any solution, and I didn't get how to do it looking at the documentation.
PHPExcel will show date formats according to the defined format mask. There are a number of helper functions available that will allow you to convert an Excel timestamp value to a PHP/unix timestamp or to a PHP DateTime object: like PHPExcel_Shared_Date::ExcelToPHP() and PHPExcel_Shared_Date::ExcelToPHPObject()