开发者

How to correctly align tabbed CSV data text files using C#?

I have the following stringBuilder that I am using as part of Log4net log layout.

var sb = new StringBuilder(DateTime.Now.ToString(DateFormatString, CultureInfo.InvariantCulture));
            sb.Append("\t");
            sb.Append(log.EventAction);
            sb.Append("\t");
            sb.Append(log.Id);
            sb.Append("\t");
            sb.Append(log.MessageId);
            sb.Append("\t");
            sb.Append(log.CodeBlock);
            sb.Append("\t");
            sb.Append(log.Details);

I would expect the \t (tab) to give me an accurate tabbing as deliminator, so that when visually inspecting the text file, the columns would align. However this is not the case!

The columns are not aligning, and when I open up the text file in MS Word and "Display Special Characters", I see that the same tab entries seem to exist, but visually in notepad, columns don't align.

I'm really confused, maybe I need to review the definition of tab, can anyone provide insight or workaround to this issue?

Update:

I've noticed that if I have strings of identical length then the columns align, so for example:

string1(len5) TAB string2(len5) TAB string3(len5)
string1(len5) TAB string2(len5) TAB string3(len5)
string1(len5) TAB string2(len5) TAB string3(len5)

However if the strings differ in length, tab char is present, but strings don't align:

string1(len5) TAB string2(len5) TAB string3(len5)
string1(len10) TAB     s开发者_StackOverflowtring2(len5) TAB string3(len5)
string1(len5) TAB string2(len5) TAB string3(len5)
string1(len10) TAB     string2(len5) TAB string3(len5)

Something like this...


A tab is just a logical separator. It has no magic ability to force the data to align, and it depends on how your viewer interprets tabs; it might mean "indent 4 characters", it might mean "pad rounding to the nearest 8th character". As such, it is normally very dependent on the existing data lengths. In Word it will usually be interpreted as "move to the next pre-defined tab", but that depends on a: what tabs are defined in Word, and b: how wide the data is already (i.e. what is "next").

In Excel (or any spreadsheet) it will generally be interpreted as columns. For this reason, a spreadsheet is probably your best option for browsing tabular delimited data (if you care about columns).

So; what viewer are you using?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜