Lukas Bystricky
Lukas Bystricky

Reputation: 1272

CSV printing issue

I have a program the writes to a csv two columns. One is a ratio, of the form 1:100, or something similar, and the other is a score.

These values are stored in a Dictionary and printed as follows:

foreach (String s in p.normalizedScore.Keys)
{
       sw.WriteLine(s + DELIM + p.normalizedScore[s]);
}

where sw is a StreamWriter and DELIM is a comma. The outputs is as follows:

1:10,7.498378506

0.111111111,18.46320676

0.736111111,30.08283816

1:10000 ,40.80688802

1:100000 ,51.93716854

1:1000000,62.89993635

1:10000000,73.54010349

The scores are all correct, but 2 of the ratios are printed incorrectly (it should be increasing 10 fold, so there should be a 1:100 and a 1:1000). When I enter the debugger, I find that at the time of printing, it's still reading all the ratios correctly, meaning I can't locate any place in my code where the ratios are wrong. Does anyone have an idea as to what the problem might be?

Edit: The above output was copied directly from Excel, but if I look at it in Notepad, the data seems fine, so it seems to me the problem is with Excel. (Still don't know what it is mind you.)

Upvotes: 0

Views: 125

Answers (2)

dmoola
dmoola

Reputation: 11

Do not just double click the file and open in excel. Open a new worksheet and import from text file. You will then specify it's a column of text - or you can create your csv with a text qualifier and use that in the import as well.

alternatively you can add a space in front of your s variable.

sw.WriteLine(" "+ s + DELIM + p.normalizedScore[s]);  

' 1:10' won't be treated as an expression.

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

As per the comments - this was an Excel formatting issue, not a code issue.

Adding Cory's comment to the answer because I think it adds significant value:

If you put quotes around your ratios, Excel shouldn't try any funny business with formatting (you won't see the quotations in Excel, that's just part of the CSV spec for qualifying your values).

Upvotes: 1

Related Questions