How to vertically align comma separated values in Notepad++?
Asked Answered
B

6

39

As shown in the picture "Before" below, each column separated by comma is not aligned neatedly. Is there any method to align each column vertically like the display effect in Excel?

The effect I wish is shown in the picture "After". Before After

Thanks to @Martin S , I can align the file like the picture "Method_1". As he has mentioned, some characters still cannot align well. I was wondering if this method could be improved? Method_1

Boeke answered 18/7, 2017 at 11:10 Comment(0)
C
41

You can use the TextFX plugin:

  • TextFX > TextFX Edit > Line up multiple lines by ...

Notepad++ > Line up...

Note: This doesn't work if the file is read only.

http://tomaslind.net/2016/02/18/how-to-align-columns-in-notepad/

Update 2019: Download link from SourceForge

Conclusion answered 2/11, 2017 at 10:51 Comment(11)
Thanks so much! Your answer is just what I want! This plugin helps me not to use other softwares(e.g. Excel).Boeke
I tried to use TextFX in my 64-bit Notepad++ and got an error that it was a 32-bit plugin and not compatible. Is there a 64-bit version?Grebe
any alternative for npp 64x?Swanson
I have notice that the TextFX plugin no longer shows up after about npp v7.5.6. So I downgraded back to that, and the TextFX plugin shows up again.Wirra
It goes out of memory on a 100k+ lines file.Kroon
My suggestion would be to use a different tool that does have an up-to-date plugin for this, e.g. VSCodeOratorian
ElasticTabs is your friendPlatinous
This is not available in plugins admin menu anymore. I am not comfortable installing directly for security reasons.Clonus
Unfortunately TextFX is now considered Abandonware due to the original author no longer supporting or working on it. The best solution out there at the time of writing is CSV Lint which is in many of the other answers on this post.Payable
There is now a new version. NppTextFX2 available in notepad++ Plugins Admin section for download.Payable
@CodeNovice NppTextFX2 does not have Line Up feature menu item.Keene
O
21

Maybe not exactly what you're looking for, but I recently added a CSV Lint plug-in to Notepad++ which also adds syntax highlighting for csv and fixed width data files, meaning each column gets a different color so it's easier to see.

CSV Lint syntax highlighting columns

Overset answered 26/10, 2021 at 14:2 Comment(4)
Lifesaver! Working on a server without Excel and your plugin for Notepad++ is very useful!Eden
Alternative for viewing CSV on a server without Excel is using PowerShell's Out-GridView, but this is slow for a larger CSV file. Import-csv -LiteralPath $file.FullName -Delimiter $CSVDelimiter -Encoding UTF8 | Out-GridView -Title $file.FullNameEden
fyi opening large files was slow in older versions of the plugin, but as of v0.4.5 there was a big performance increaseOverset
to be clear, CSV Lint can vertically align the dataCalpe
W
16

Notepad++ CSVLint

  • Install CSVLint Plugin (Plugins > Plugins Admin... > CSV Lint)
  • Open CSV file. Or manually set Language > CSVLint. This will give you nicely colored output.

enter image description here

To reformat do this:

  • Open lower pane: Plugins > CSV Lint > CSV Lint Window.
  • Click the Reformat button. Check the box Align vertically (not recommended). -- This may screw up your data, so think twice before clicking OK.

enter image description here

Reformatted output: enter image description here

FYI: You can use the Trim all values checkbox to reformat back.

If you want to try this yourself: Here is my sample input:

TIMESTAMP_START,TIMESTAMP_END,TA_ERA,TA_ERA_NIGHT,TA_ERA_NIGHT_SD,TA_ERA_DAY,DA_ERA_DAY_SD,SW_IN_ERA,HH,DD,WW-YY,SW_IN_F,HH
19890101,19890107,3.436,1.509,2.165,6.134,2.889,100.233,283.946,1.373,99.852,2.748,1.188
19890108,19890114,3.814,2.446,2.014,5.728,2.526,91.708,286.451,1.575,100,100.841,0.742

Notepad++ plugins are fragile. This worked for me on Notepad++ v8.4.2 (32-bit). So if this doesn't work you, then consider switching from 64-bit to 32-bit and also downgrading to this exact version and bit-ness.

Related: CSVLint demo video, CSVLint Documentation on GitHub

Wayzgoose answered 31/1, 2023 at 12:1 Comment(5)
Thank you, this is available in 64-bit whereas TextFX is not.Plugugly
@RevitArkitek: I have given up on NPP x64. I had too many problems with plugins. So for me it's gonna be all NPP x86 until something FORCES me to change.Wayzgoose
BEST Answer... you can even reformat the data back if needing to. Such a perfect plugin to work with CSV data. I've been dreaming about something like this for years. I thought this would ever only exist in my dreams. Thank you to the community for informing me of this amazing plugin. I hope it doesn't die off like TextFX Plugin did. I'll pour some out for the homies.Payable
@CodeNovice: Try with 32bit NPP => Plugins Admin => "NppTextFX2". Works nicely. (Seems to be a fork or something: github.com/rainman74/NPPTextFX2)Wayzgoose
@Wayzgoose I Just used the Admin Plugins page in Notepad++ to install and voila. Thank you very much for the heads up. Glad to see it back!Payable
D
9

You can use this python plugin script which utilizes the csv library which takes care of quoted csv and many other variants.

Setup:

  1. Use the plugin manager in Notepad++ to install the "Python script" plugin.
  2. Plugins->Python Script->New Script (name it something like CSVtoTable.py)
  3. Paste the following python script into the new file and save:

CSVtoTable.py

import csv

inputlines = editor.getText().split('\n')
# Get rid of empty lines
inputlines = [line.strip() for line in inputlines if line.strip()]
reader = csv.reader(inputlines, delimiter=',')
csvlist = [line for line in reader]
# transpose to calculate the column widths and create a format string which left aligns each row
t_csvlist = zip(*csvlist)
col_widths = [max([len(x) for x in t_csvlist[y]]) for y in range(len(t_csvlist))]
# To right align - change < to >
fmt_str = ' '.join(['{{:<{0}}}'.format(x) for x in col_widths]) + '\r\n'

text = []
for line in csvlist: 
    text.append(fmt_str.format(*line))

# open a new document and put the results in there.    
notepad.new()
editor.addText(''.join(text))
  1. Open your CSV file in notepad++
  2. Click on Plugins->Python Script->Scripts->(The name you used in step 2)
  3. A new tab with the formatted data should open.

Update (right aligned numbers & left aligned strings):

Use the following python script if you want to right align number fields from the CSV - it looks at the second line of the csv to determine the types of the fields.

import csv
import re

num_re = re.compile('[-\+]?\d+(\.\d+)?')

inputlines = editor.getText().split('\n')
# Get rid of empty lines
inputlines = [line.strip() for line in inputlines if line.strip()]
reader = csv.reader(inputlines, delimiter=',')
csvlist = [line for line in reader]

# Transpose to calculate the column widths and create a format string which left aligns each row
t_csvlist = zip(*csvlist)
col_widths = [max([len(x) for x in t_csvlist[y]]) for y in range(len(t_csvlist))]

# Numbers get right aligned
type_eval_line = csvlist[1 if len(csvlist)>1 else 0]
alignment = ['>' if num_re.match(item) else '<' for item in type_eval_line]

# Compute the format string
fmt_str = ' '.join(['{{:{0}{1}}}'.format(a,x) for x,a in zip(col_widths,alignment)]) + '\r\n'

text = []
for line in csvlist: 
    text.append(fmt_str.format(*line))

# open a new document and put the results in there.    
notepad.new()
editor.addText(''.join(text))
Dogcart answered 28/11, 2018 at 23:47 Comment(1)
The only plugin I see for Python is Python Indent. I don't see Python Script in the list of plugins.Silures
D
4

You could use Search&Replace to change all occurrences of , to ,\t. This will add a tab after each ,.

This method has however some drawbacks:

  1. you effectively add white-space characters to your document (in case you need to edit and save it).
  2. This works well only if the difference (in terms of number of characters) between the longest and the shortest numbers is less than 1 tab-size (usually 4 characters).
Dysgraphia answered 18/7, 2017 at 12:0 Comment(4)
Thanks! Now the display effect is quite better, although there are some that cannot align vertically because of the reason you mentioned. Is there some other method to improve it ?Boeke
Well, let's say that Notepad++, like any other text editor, is not really the right tool to use for what you want to achieve... If you are for some reason bound to Notepad++, the only further improvement I can think of is using Search&Replace with regular expressions: making multiple iterations to search for strings of length n and pad them with m leading spaces, than search for strings of length n+1 and pad them with m-1 leading spaces, and so on....Dysgraphia
What you say "Notepad++, like any other text editor, is not really the right tool to use for what you want to achieve" truly enlightened me ! Now I use Excel to align vertically and it works well ^_^.Boeke
You can also use the Elastic Tabstops Plugin that is included in NPP to address drawback number 2.Ergotism
K
0

As noted in the comments not TextFX (32-bit only, Line Up feature has a not expected formatting), nor NppTextFX2 (64-bit only, Line Up feature is removed).

Another solution is ElasticTabstops: https://github.com/mariusv-github/ElasticTabstops (archive: https://github.com/dail8859/ElasticTabstops)

Side works:

To test:

  1. Replace column separator character to tabulation character or, for example, copy table from https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B into new tab.
  2. (optionally) Check plugins->Elastic Tabstops->Enable, plugins->Elastic Tabstops->Selection only to convert into table view on-a-fly for a selected text. You can even use a block selection (ALT+SHIFT) to immediately select a formatted piece of text.
  3. Select plugins->Elastic Tabstops->Convert Elastic Tabs to Spaces
Keene answered 16/4, 2024 at 5:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.