I have a dictionary that I want to write into the CSV file. while writing the string value it becomes float. but I need the same string value in CSV file not float. any idea?
mydict={'date':int(20200729),'number':int(123),'code':int(707),'cipher':str('54545417e92')}
print mydict.values()
with open('formatting.csv','ab') as f:
w=csv.writer(f)
w.writerow(mydict.keys())
w.writerow(mydict.values())
Manually increase the width of the column and you'll see the format changes.
Since you are writing a csv file (which, unlike xlsx, does not contain its own styling and formatting), it's not related to Python and there's nothing Python can do to make Excel use a specific format.
Like DeepSpace said, this comes from what excel is doing, not from what python is doing. But from my experience, once excel has opened the file and assumed your data to be a float you cannot get that precision back. I suggest viewing your data raw by opening the .CSV file with a text editor instead of excel.
If you must open the file in excel, then there is a different way to do it. Open a blank excel document and then go to the data tab and click "From Text/CSV". Then follow the prompts and use the wizard to import your data. This way you can make sure the data type does not change from string to float.
EDIT - as a side note, I see that you tagged your question with "python 3.x", but in your example you use the old python 2 syntax of print "a string". Starting in python 3.0, you must use print("a string").
Related
I have a .csv file with a location column. For building 8, room 103 I know that the value in the source database is 8-8103. When I open this file in Excel it converts this to 8/1/8103 and displays "Aug-03". That makes sense, I see why it converted it (the Location column =YEAR(Equipment Location))
What I CANNOT understand is when I read this same file into Pandas, use open(file) file.read() or just open it with Notepad++ the value that I see is "Aug-03".
How is it possible that the literal string value in a .csv appears in the function line of Excel, but shows as a wrongly converted string in programs that are reading it as text?
To add to this confusion, Excel is able to display 8/1/8103 and 8/1/8203 in the function bar and displays them both as "Aug-03". If the underlying string is Aug-03, how is it able to make that distinction?
I hope this makes sense to somebody,
Thanks.
I have a Python script that creates a CSV file, and one of the columns has values such as 4-10, 10-0, etc.
When I open the CSV in Excel it's formatting these values as dates, ex, 4-Oct. When I go to Format Cells and change the type to Text, it changes 4-10 to 43012.
What's the easiest way to stop this?
When you import the data into Excel, tell the Import Wizard that the field is Text.
My preference is to deal with the inputs, when possible, and in this case if you have control over the python script, it may be preferable to simply modify that, so that Excel's default behavior interprets the file in the desired way.
Borrowing from this similar question with a million upvotes, you can modify your python script to include a non-printing character:
output.write('"{0}\t","{1}\t","{2}\t"\n'.format(value1, value2, value3))
This way, you can easily double-click to open the file and the contents will be treated as text, rather than interpreted as a numeric/date value.
The benefit of this is that other users won't have to remember to use the wizard, and it may be easier to deal with mixed data as well.
Example:
def writeit():
csvPath = r'c:\debug\output.csv'
a = '4-10'
b = '10-0'
with open(csvPath, 'w') as f:
f.write('"{0}\t","{1}\t"'.format(a,b))
Produces the following file in text editor:
And when opened via double-click in Excel:
The first few lines of my CSV file look like this (when viewed from Notepad++):
Trace,Original Serial Number,New Serial number
0000073800000000097612345678901234567890,0054,0001
When I open this file in excel, I get this:
For some reason, excel is truncating the serial numbers and the trace number. I have tried changing the format to Text but that still doesn't work, as excel only sees the value up to the 6:
7.38000000000976E+34
If I change it to Number:
73800000000097600000000000000000000.00
What can I do? I only have 60 lines, so if I have to start over and some how recopy the text into excel I will, but I'm afraid saving it will change the format once again.
You shouldn't need to start over or alter the existing CSV. The fastest way might be to use Excel's text import wizard. In the data tab under Get External Data click From Text and select your CSV file.
The wizard that appears will let you tell Excel the data type of each "column" and you can tell it to use text for your barcode.
Excel is trying to "help" you by formatting the input values. To avoid this, do not double-click the file to open it. Instead, open the Data tab and in the Get External Data section, click on From Text
Then tell the Import Wizard that the fields are Text:
One solution that may work for you depending on the environment you consume the csv, you can add a nonnumeric character to the beginning and end (e.g. a "_") of the values. This will force Excel to recognize it as text. You can then remove the "_"s in your downstream environment (SQL, Databricks, etc.) or even keep them if they don't interfere with your reporting.
Funny thing when I want to save Excel columns with long numbers like below, resulting csv contains converted numbers to scientific notation which is unusable. I want them to be save as text. Any trick to do that?
28160010390002003505456159
12160010390002003505456156
39160010390002003505456155
39160010390002003505456155
Append a TAB char (ASCII 9) at the end of a number.
In order to have those long (>15 digit) numbers in Excel, they are already formatted as text. I suspect that the .csv file also shows them as long numbers (if you open the csv file with Notepad), but that when you open the csv file in Excel, you see them as truncated and converted to scientific notation.
If that is the case, what you need to do is IMPORT the csv file. When you do that, the text-to-columns wizard will open, and allow you to format that column as text. The location of the Import is different in different versions. In Excel 2007, it is on the Data ribbon, Get External Data / From Text.
put the number as a function like below:
="123456789123456789"
If, as the original question seems to imply, you are actually:
already working with data in Excel, and
want to save to a CSV format without losing digits in an extra long number,
Then, before doing a 'Save As' to your CSV format, try formatting the column with a custom format, and in the box for the pattern just type #. This will force Excel to see it as a number, however many digits long, without trying to do something else with it like 4.52364E+14 when you actually save it to CSV.
At least, that is how it works for me in Excel 365 at this point in time.
If you are trying to get data into Excel from a CSV, then the answer about using the data import wizard is probably the safest bet instead.
This is an old question, but since at the moment it's still the top result on a google search for the topic, I think the thread should be kept current. Hussein mahyoub provided the only real answer to the question, yet has not gotten the up-votes.
The answer which tells us to add a tab character after your number gets you an string of text with a tab after it inside excel. It looks visually correct, but, it's not correct inside the spreadsheet. If the intent is to use the data in excel as excel data and use formula etc, it could cause problems. Interestingly if you put the tab before the text, it shows up in the data after the text.
The answer which tells us inserting a ' before the text gets a ' in the excel file. It's visually an incorrect representation of the data.
The answer which boasts the virtue of openoffice is simply an evil troll which does not even attempt to answer the question.
The answer that explains how to import a CSV that has not been properly formatted is good information, but, off topic.
The direct answer to the question is
converted to text,largest number
="123456789012",12345678901
Convert the numeric to text using text function.
Text(number,"0")
using openoffice you can save in csv format without problems.
Don't use Export to csv feature in Excel.
You can easy format that column to number in Excel, then use "Save as" it with csv and "yes" to confirm that you want to keep the format in csv.
That is work for me
Click on the column that has scientific exponent number and go to Format cells and then Numbers (decimal point as 0), save it as MSDOS CSV. Worked for me.
Is this at all possible?
If I open up my file in standard text editor e.g. notepad the preceeding zeros are displayed.
e.g. 000485001 shows up.
Although this doesn't happen in excel. All that's displayed is 485001
Just wondering if there's a way around this?
Thanks,
Yes, when you're importing (or using 'Text to columns') you can explicitly indicate the data type for a column (instead of General). If you select 'Text' the zeros will not be dropped.
Unfortunately you only see the dialog to specify this option when Excel is already open and you use either File/Open or Data/Text to Columns. If you just double click a .csv in the explorer you don't get this choice.
Excel tries very hard to determine the type of value it's importing. If it looks like a number, it will treat it like a number, and drop all the leading zeros as it reads it in. There's no way to get them back once they're lost.
You might try to import the file using the wizard that lets you set the data type for each column.
Rather than writing your data as a CSV file, use the SYLK (Symbolic Link) format instead. This format includes information about the style of a column, so that Excel will not try to auto-guess the type of data.
The easiest way to get started with this format is to export a small file from Excel and use that as a template.
Ok got around this by inserting a text character before the number i.e. #000485001
Simple enough!