CSV Showcase

ZorkFanDM
2010-09-23
2013-01-25
  • ZorkFanDM
    ZorkFanDM
    2010-09-23

    This is a quick script to make reading through CSVs in notepad++ easier.
    I often just need to quickly look down for a value, and opening Excel or OOCalc is time consuming.

    a = [[] for x in editor.getLine(1).split(",")]
    def csv_to_list(cont, l_num, tot_lines):
        cont = cont.split(",")
        for n in range(len(cont)): a[n].append(cont[n].replace("\r\n", ""))
    editor.forEachLine(csv_to_list)
    aLens = [max([len(x) for x in a[y]]) for y in range(len(a))]
    def form_line(l_num):
        txt = "".join([a[x][l_num].ljust(aLens[x]+1) for x in range(len(aLens))])
        txt = txt + "\r\n"
        return(txt)
    notepad.new()
    for l in range(len(a[0])-1): editor.addText(form_line(l))
    
     
  • vos causa
    vos causa
    2011-03-08

    I received an error while trying your script :

    Python 2.7.1 (release27-maint-npp, Feb  6 2011, 16:58:20)
    Initialisation took 609ms
    Ready.
    Traceback (most recent call last):
      File "C:\Users\Robert Vos\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\csvsplit.py", line 6, in <module>
        editor.forEachLine(csv_to_list)
      File "C:\…\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\csvsplit.py", line 4, in csv_to_list
        for n in range(len(cont)): a.append(cont.replace("\r\n", ""))
    IndexError: list index out of range

     
  • That appears to occur if you've got one (or more) lines with a different number of fields in - bear in mind also that the script doesn't cope with quoted data - so

    "hello","there","jones,dave"

    The last "field" (jones,dave), would be treated as two.  Perhaps someone, maybe the original author, can suggest an improvement to the script…

    Cheers,
    Dave.

     
  • vos causa
    vos causa
    2011-03-09

    Thanks for the reply. My data contains variable lenght records (a variable number of columns) and is quoted.
    I have tried to solve it with Notepad, making use of  Text Fx edit. Bbut (I think) because I use UTF-8 encoded data, the aligment function (Line up by multiple lines) does not work correctly.
    I have solved the readability with: uniCSVed, but now I miss my other Notepad++ functions.