CSV file creation without exponential numbers - excel

How do you get around the exponential conversion that takes place when inserting a value into a csv file. I have a process that creates a csv file and then starts entering rows into it. One of those fields inside a row inserts a value similar to this:
123,45,45,466,6656,23423,2455,234,2454
These are just a string of id's i need to preserve on the csv file in order to import them into another program that expects a comma separated number of values in this field.
Of course when i open excel and look at this csv it gives me something like this:
123,45,45,466,6656,000,000,000,000
so those last few values mean nothing to the import process and it fails.
My question is how do i write to a csv file and get around my values being converted to exponential numbers as well as preserving the comma separated number values like above?
Thanks in advance
Billy

Put quotes around them
"123","45","45","466","6656","23423","2455","234","2454"

In order to get around this issue i used the following steps in MS Excel 2007:
Open a Blank Workbook in Excel.
Choose Data, Get External Data, Import Data. (Excel 2007 is Data, Get External Data, Data from Text)
Browse to your .csv file and Select "Import".
Import Wizard should appear.
Page 1 Select "Delimited"
Select the row which you want to start the import.
click "Next"
In the Delimiters, select "comma" and/or other delimiters you are using.
Note: The bottom half of the window will preview the way the data is to be imported.
click "Next"
highlight each column of your data in the window below. For each column you can specify "General", "Text", "Data", or "do not import column" using the radio buttons in the top left of the Wizard box. I used the "Text" option. This is an optional step.
Click Finish.

place the value in single quotes while using data
Eg:
String[] abc = {"0123455677888", "154758768969", "abcsdhfsdj", ""abcsdhfsdj3725423", "62343664268abcsdhfsdj"};
CSVWriter csvWriter = new CSVWriter(String fileName);
//csvWriter.writeNext(String[] stringArray)
csvWriter.writeNext(" \' "+abc[0]+" \' ", " \' "+abc[1]+" \' ", abc[2], abc[3], abc[4]);
Try this
It will store the data perfectly

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

KDB: Preserve Leading Zeros When Saving Data to CSV?

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.

How to import CSV-file with Microsoft Excel 2016?

I try to import a CSV-file in Excel. The program has funny default properties. It puts the content of the rows all in one column including the commas. Lets say the file content is (test.csv):
1, 2, 3
4, 5, 6
What I expect Excel to do is to put each number into a separate field using the comma as separators. However, the excel table has only two fields each containing 3 numbers including the commas, something like this (not what I want):
"1, 2, 3"
"4, 5, 6"
What do I have to change in Excel settings so that it does this standard task correctly?
You are probably not importing the CSV but instead opening it either double clicking or using the open dialog.
One of the correct ways of importing a CSV into Excel is using Data | From Text/CSV menu. This way you will be able to select the delimiters, change data types if you need etc.
Importing CSV via Data | From Text is -- altough working -- rather cumbersome: You need to select the file, the wizard asks about "Delimited" or "Fixed width", then you need to select "Comma" as seperator and finally how to import the data. And when you want to reimport later on the whole procedure starts again and again and again ..
Instead use Data | New Query | From File | From CSV and select the file to import from. I still had to adjust the encoding to UTF-8 while other settings are correctly detected (delimited by comma).
The really great advantage is that you have to do this only once as the above procedure creates a so called "workbook query". To update or reimport just click "Refresh" in the workbook query's context menu.
Depending of the configuration of Excel, the CSV files can be separated with "," or ";". To open a CSV file it's better to go to file-->open file-->text. There Excel asks you for the way you want to separate your fields in CSV.
Regards

Importing CSV into Excel: import wizard fails to recognize text qualifier but opening directly in Excel converts to scientific notation

I receive a csv file periodically from a database download and I cannot change the way this csv file is produced. I need to convert it to an Excel file to pass it on to an external partner. There are two fields causing trouble in the csv file when I import into Excel: a permit number which is actually a character string, and a location description which can contain commas and other punctuation.
If I right click on the csv file and select Open With Excel, the location description is imported correctly but some permit numbers are converted to scientific notation.
Alternatively, if I use the text import wizard in Excel, I can import the permit numbers as text, but the location description ends up being split across multiple cells even when I set the text qualifier to ".
Here is a simplified example of the data as it appears in the csv file:
id, "Permit Number", Species, Longitude, "Location Details"
1, 2F66-16, DE, -120.0001, "near a road, in woods (FR16)"
2, 678E-15, DE, -120.0002, "near milepost 65, north side of road"
Is there a solution that will allow me to import both fields correctly? This question is very similar but has no answers:
excel text importing: wizard vs opening a file within Explorer
Thanks for any help.
Update: I think the Location Description is being split into multiple cells because of line breaks in the csv. I'm not sure how these are being generated or how to remove them or deal with them, but when I open the file in Excel, the line breaks are still there (although the entire location description is in one cell). Unfortunately I can't post the full example data because it is sensitive data not easily converted to dummy data. Any thoughts on how to remove line breaks from a csv file?
For whatever reason, Excel doesn't seem to like spaces between the comma and the quote. If you do a global find and replace on your input data (change comma-space-quote to comma-quote) -- and then parse your data, it will honor the text qualifiers as you need.
id, "Permit Number", Species, Longitude, "Location Details"
1, 2F66-16, DE, -120.0001, "near a road, in woods (FR16)"
2, 678E-15, DE, -120.0002, "near milepost 65, north side of road"
You are on the right track using the text import wizard within Excel, that at least gives you more control.
I don't know why the text qualifier isn't working, that's exactly what it is designed to do.
Are you specifying that final column as TEXT instead of leaving it as GENERAL?
Do the sample items in the wizard show correctly?
Are you positive they are " characters, and something similar (like smart quotes)?
It's a pain, but if nothing else works, you can always combine the columns back together at the end:
Insert a column before your broken up details
Add a formula like =E2 & "," & E3 (or longer if it splits into more than two)
Copy the combined column, then paste values (over the same cells)
Delete the now extraneous columns
Was able to do it with the text importer:

Lost precision when saving csv to excel format

I have a CSV file, in which one column is storing some account number text, like 99010124450000237, however when i try to save it to excel, it always format the number to 99010124450000200, the last 2 digits are rounded. this is so annoying, because actually i just want the account number be text field, but Excel will always take it as a number, and even worse, it was rounded. anybody have the same pinpoint and maybe some suggestions?
I tried to format the column to a specific format, and tried to import data. neither method works.
You don't say what version you're using, but the following seems to work in Excel 2007:
Use Data ... Get External Data ... From Text Select your file and
click "Import"
In Text Import Wizard Step 1, set Original Data Type
to "Delimited" (the default on my installation)
In Text Import Wizard Step 2, select your delimiter (my test file uses Comma)
In Text Import Wizard Step 3, click on the column with the account
number (it should be highlighted) and change "Column Data Format" to
"Text"
Click "Finish"
I used a file with the following text, selecting the second column to be Text:
"99010124450000237",99010124450000237,"99010124450000237"
99010124450000237,"99010124450000237","99010124450000237"
and got this output:
A B C
1 9.90101E+16 99010124450000237 9.90101E+16
2 9.90101E+16 99010124450000237 9.90101E+16

Resources