having trouble with date formats in CSV file format(Excel)
I want the date in "YYYY-MM-DD hh:mm " format and I need it in .CSV format.
When reopening the file, the date format gets changed and it looks like "MM-DD-YYYY hh:mm".
Does anyboy know how I can retain my desired date format when saving the file as .CSV?
Thank you!
Use the format YYYYMMDD in all CSV files, which doesn't convert to date in Excel or you can do one of the below things
Use a Custom format, rather than one of the pre-selected Date formats, the export to CSV will keep your selected format. Otherwise it defaults back to the US format
Place an apostrophe in front of the date and it should export in the correct format. Just found it out for myself, I found this thread searching for an answer.
Change the date and time settings for your computer in the "short date" format under calendar settings. This will change the format for everything yyyy-mm-dd or however you want it to display; but remember it will look like that even for files saved on your computer.
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.
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 have a .csv file that is created generated using some legacy software. There is no way to look inside the code at how or what its doing to produce the csv file.
My issue is that the date field format is behaving strangely.
The .csv file is storing the date in the following format.
MM-dd-yyyy HH:mm:ss.SSS
Looking in the file in notepad (raw data) we get a list of columns like this:
"01-12-2013 14:23:38.000",146.199997
"01-12-2013 23:20:29.000",146.199997
"01-13-2013 17:59:23.000",152.299988
"01-13-2013 18:08:40.000",147.300003
"01-13-2013 18:25:52.000",156.000000
"01-13-2013 18:35:14.000",146.300003
"01-13-2013 18:44:56.000",148.500000
"01-13-2013 18:56:41.000",151.699997
NOTE: I have stripped out the remaining columns to make it easier on the eye so we are only seeing the first 2 columns in the .csv file.
Now when I open this file in Excel 2010 version 14.0.6023.1000 (32-bit) I get date format problems.
Firstly if I just open the file I get blocks of mixed date formatting like this:
And this:
I can see that Excel is getting confused when the day value is switching from 12 to 13. If i then try to change the formatting to fix the issue using the following cell formatting settings:
Then I get the following displayed in the column when the day is less than 13:
And the following format when the day is greater than or equal to 13:
Any help would be really appreciated on this as I am now completely out of ideas.
NOTE: I have also tried copying the contents of the .csv file into another file but the same things occur in the new file.
This problem is to do with how Excel is interpreting the date format (M/D/Y vs. D/M/Y). It appears your system is setup for D/M/Y date formats. You won't find the setting in Excel because it derives it from your system settings. Go to the Language settings and add US English. Then set it to default and try opening the CSV file again.
Alternatively you can paste the csv file contents into a new sheet and use the 'Text-to-Columns' feature, which will let you specify the date format.
You have a conflict between the format of the .csv and your Regional settings.
First import the data as Text,(rather than double-clicking). Then in B1 or some other column enter:
=DATE(MID(A1,8,4),MID(A1,2,2),MID(A1,5,2))+TIMEVALUE(MID(A1,13,8))
and apply the desired formats:
I found a work around for my instance after reading the answers from #Gary's Student and #hughg. Thanks for the ideas.
So to fix my issue:
Modified my regional settings for dates and times to US.
Opened the file as normal in excel.
Selected the column in question and formatted cells into text. This converted all the dates in the file into floating point numbers.
Finally Apply following custom format to the cells: dd/mm/yyyy hh:mm:ss
The result is that every date in the file is now correctly formatted into UK date format.
I have a report that uses a single query as record source to generate a report.
I am exporting that report into Excel with following code
DoCmd.OutputTo ObjectType:=acOutputReport, ObjectName:=strReport, OutputFormat:=acFormatXLS, outputfile:=strpath & strTempFile, AutoStart:=False
All the data in the report is correct except for date, where date format is displayed as Genereal Number (Not sure which format)
Any help is appricaited.
Thanks.
Excel is displaying the date to you in the way excel 'sees it', rather than in a user friendly date format.
In excel, every date has a value that is a number. This allows excel to perform calculations/do things with dates (example - checking the days between two dates).
If you reformat your excel doc (right click on a random date and click format and then change to date) you'll probably find you have the correct date exported.
Probably haven't answered your question, but if you're just doing a data dump then you may not need to export the formatting (because you can just simply show the date in date format on your report).
i have problem with excel changing the date format so it fits regional settings.
my problem is:
i am creating tool in which user chose which *.csv file to import and work with. While tool is on user side i can't rely on regional setting - tool have to work for all.
Problem is that excel automatically change date format to fits regional setting and therefore it is wrong or i can't parse it with static delimiter (like '/').
I know the format in which the date is written in the csv file (yyyy-mm-dd or dd/mm/yy) and i would like to read that date as a text so i can use DateSerial to create correct date.
Is there any way to read csv only as text and prevent excel from changing it to date (based on user regional settings) automatically ?
Thank you.
When importing, specify the column as text.
Or, put an apostrophe at the start of each date value. This is the "this is a text value, don't reformat it" symbol.
example
123,'12/29/1999,blah blah