How to vertically align comma separated values in Notepad++?

杀马特。学长 韩版系。学妹 提交于 2020-12-30 04:58:23

问题


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?


回答1:


You can use the TextFX plugin: Edit > Line Up multiple lines by ...

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




回答2:


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))



回答3:


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).


来源:https://stackoverflow.com/questions/45165105/how-to-vertically-align-comma-separated-values-in-notepad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!