Reputation: 613
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".
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?
Upvotes: 45
Views: 171113
Reputation: 2727
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:
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.plugins->Elastic Tabstops->Convert Elastic Tabs to Spaces
Upvotes: 0
Reputation: 3102
To reformat do this:
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
Upvotes: 19
Reputation: 1693
You can use the TextFX plugin:
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
Upvotes: 43
Reputation: 3048
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.
Upvotes: 25
Reputation: 109
You can use this python plugin script which utilizes the csv library which takes care of quoted csv and many other variants.
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))
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))
Upvotes: 9
Reputation: 884
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:
Upvotes: 4