KDB: Preserve Leading Zeros When Saving Data to CSV? - excel

I'm trying to export data from KDB to a csv file. Unfortunately, when I do this, the leading zeros disappear. For example, 0355 becomes 355.
Is there a hack of sorts that I can use to get around this issue?

I think your issue may be in the process of opening the CSV with excel. The prevailing 0's are still present until viewed in excel.
If you are wanting to view the data in excel one option would be to first string the data and then add a prevailing ' in kdb. For example:
q)table: flip `a`b`c!(`001`002`003; 123; 10 12 12)
q)update a:("'",'string a) from table
a b c
-------------
"'001" 123 10
"'002" 123 12
"'003" 123 12
This effectively acts as a indicator stating "this is text", thus the 0 will be retained. The ' will not be visible in the cells unless you edit a specific cell, as a result it is useful for displaying these symbols. Other than this I think your best option would be to just change the column type settings in excel.

If your .csv file contains the leading 0's when opened in notepad, then the problem is likely that Excel is misinterpreting the value as a number since when kdb+ exports symbols to a csv file it does without wrapping the value in quotation marks.
To get around this open Excel and click on the 'Data' ribbon and then 'From Text' to import your csv using the Import Wizard. Once you have selected your csv, the wizard will open and you can set the delimeter as comma.
You will then be able to choose the data types for each column, and setting the column with the leading 0's as text format will make them appear correctly.

Related

Csv writer escape semicolon python [duplicate]

I am using Excel for Mac 2016 on macOS Sierra software. Although I have been successfully copying and pasting CSV files into excel for some time now, recently, they have begun to behave in an odd way. When I paste the data, the content of each row seems to split over many columns. Where as before one cell would have been able to contain many words, it seems now as though each cell is only able to contain one word, so it splits the content of what would normally be in one cell, over many cells, making some rows of data spread out over up to 100 columns!
I have tried Data tab>> From text>> which takes me through a Text Wizard. There I choose Delimited>> Choose Delimiters: Untick the 'Space' box ('Tab' box is still ticked)>> Column data as 'General'>> Finish. Following this process appears to import the data into its correct columns. It works. BUT, a lot of work to get there!
Question: Is there any way to change the default settings of Delimiters, so that the 'Space' delimiter does not automatically divide the data?
I found an answer! It has to do with the "Text to Columns" function:
The way fix this behavior is:
Select a non-empty cell
Do Data -> Text to Columns
Make sure to choose Delimited
Click Next >
Enable the Tab delimiter, disable all the others
Clear Treat consecutive delimiters as one
Click Cancel
Now try pasting your data again
I did the opposite regarding "consecutive delimiters"!
I put a tick in the box next to "Treat consecutive delimiters as one", and THEN it worked.
Choose delimiter directly in CSV file to open in Excel
For Excel to be able to read a CSV file with a field separator used in a given CSV file, you can specify the separator directly in that file. For this, open your file in any text editor, say Notepad, and type the below string before any other data:
To separate values with comma: sep=,
To separate values with semicolon: sep=;
To separate values with a pipe: sep=|
In a similar fashion, you can use any other character for the delimiter - just type the character after the equality sign.
For example, to correctly open a semicolon delimited CSV in Excel, we explicitly indicate that the field separator is a semicolon:
reference

Excel 2010 - load a text file "as text"

Is there anyway I can get Excel 2010 to load a text file as pure text, i.e. apply no interpretation to any field? I need to modify a large CSV file with several hundred columns (basically remove some columns). It's health data, so some of the columns are:
NHS number "123456789012", which Excel displays as 1.23E09
Hospital ID: "0123456", which Excel displays as "123456". The leading zero is important here.
Various 2 char status codes where the leading zero is imports, i.e. "01" does not mean "1".
I don't want to have to go through all 297 columns reformatting them correctly. Please help!
From main menu Data select From Text (or similar. I don't know if it is the correct label in English Excel version. On my version it is the third button from left on the Data ribbon).
Then choose your csv file. You will get a wizard where you have to choose the column separator character and especially the import format of the columns. Here you should select all columns and then choose the radio button 'Text'. This should import all data as plain text.

Export Excel data with a custom deliminator in .csv

I want to export a huge excel file as a .csv, but the data contains commas within the cells.
How do I export the excel data to a .csv with the deliminator as this |
I've tried doing the usual "save as", but it is not working for my data.
Hi Check out this method in the below link, its what I have used in the past
https://www.howtogeek.com/howto/21456/export-or-save-excel-files-with-pipe-or-other-delimiters-instead-of-commas/
Summary:
Control Panel –> Region and Language, and then click the Additional settings
When in the additional settings find “List separator”
Change this to anything you want, in your case you would want the | pipe
By definition CSV stands for Comma Delimited.
Easy alternatives that were under your nose in the Save As window are:
"Formatted Text(Space Delimited)" though this won't be appropriate if you have spaces in your cells
"Text(Tab Delimited)" it is less likely you'll have tabs in your cells and people won't type those in to excel.
If you really want the 'pipe' | then I would suggest saving as tab delimited and running some kind of find & replace on the resulting text file. This is a bit of a workaround
A even bigger workaround is to concatenate the cells using a formula on the sheet with a | involved and the save as text only that column.

Export data from Access to Excel without losing leading zeroes

I have a table in Access I am exporting to Excel, and I am using VBA code for the export (because I actually create a separate Excel file every time the client_id changes which creates 150 files). Unfortunately I lose the leading zeroes when I do this using DoCmd.TransferSpreadsheet. I was able to resolve this by looping through the records and writing each cell one at a time (and formatting the cell before I write to it). Unfortunately that leads to 8 hours of run time. Using DoCmd.TransferSpreadsheet, it runs in an hour (but then I lose the leading zeroes). Is there any way at all to tell Excel to just treat every cell as text when using the TransferSpreadsheet command? Can anybody think of another way to do this that won't take 8 hours? Thanks!
prefix the Excel value with an apostrophe (') character. The apostrophe tells Excel to treat the data as "text".
As in;
0001 'Excel treats as number and strips leading zeros
becomes
'0001 'Excel treats as text
You will probably need to create an expression field to prefix the field with the apostrophe, as in;
SELECT "'" & [FIELD] FROM [TABLE]
As an alternative to my other suggestion, have you played with Excel's Import External Data command? Using Access VBA, you can loop through your clients, open a template Excel file, import the data (i.e. pull instead of push) with your client as a criteria, and save it with a unique name for each client.
What if you:
In your source table, change the column type to string.
Loop through your source table and add an "x" to the field.
If the Excel data is meant to be read by a human being, you can get creative, like hiding your data column, and adding a 'display' column that references the data column, but removes the "x".

Excel changes date formats

I run a process to produce a rather large CSV file of data. Sometimes I find it helpful to open the CSV in excel, make changes manually, and then re-save. However, if there are dates in the CSV, excel automatically reformats them. Is there a way to prevent excel from doing this? It would be helpful if I could turn off all text formatting altogether.
If you prepend an apostrophe ' to the beginning of any date string during the export process, Excel will read the value literally (i.e. as plain text) rather than trying to convert it to a date.
This particular solution is handled during the export process. I'm not sure how you would change Excel to treat the file differently at runtime.
Excel does some nasty tricks when outputting XML. One of its tricks is to drop left most column delimiters if 16 or so consecutive rows have no values for these columns. This means that if you're splitting the lines up based on commmas then these rows will have a different number of columns to the rest.
It will also drop any initial 0's so things like numeric Ids can become messed up.
Another risk you run is chopping the file off short since Excel can only support a maximum number of rows. (Prior to Excel 2007 this was around 65536)
If you need to do anything to a CSV file other than read it use a text editor.
When you import the CSV file into Excel, be sure to pre-format the date column as text. There's a frequently overlooked option in the parsing that allows you to control the format column by column. This also works well for preventing the leading zeros in New England ZIP codes from getting dropped in your contact lists.
If you used the excel file version which is 2010 or later (not sure lower version), you can set up to use current operation-system date format or not in Excel/CSV file.
Right Click cell with date value (e.g. '9/12/2013') in CSV file and pop up the menu
Click 'Format Cells' and open a pop up screen
Go to 'Number' tab and you can see 'Date' was selected in 'Category' (left side) and 'Type' on the right side
Observed that there are two types of Date format (one is with () and another is not with ()). Read the comment there and you can find that you can use the date format which is not with date. It means that your changes to the CSV file will not be applied with your current operation-system date format. So, I think date format won't be changed in CSV file in this case.

Resources