0

Working on Windows 10 with cmd encoding cp852, with code:

import time,os,os.path as os_path, sys,psutil

def current_path():
    return os_path.dirname(sys.argv[0])+'\\'

PATH=current_path()

def string_line_spacing(keys,vals=None,newline=True,dashUp=True,dashDown=True):

    print '#'*20
    print keys,vals
    if vals is not None:
        keys%=vals

    keys=keys.split(',')

    ln = len(keys)


    if ln:
        st =(' | '+' \t | '.join(keys)+' \t | ').expandtabs(23)
        print st
    else:
        st=''



    if newline:
        st+='\n'

    if dashUp and dashDown:
        st='-'*len(st)+'\n'+st+'-'*len(st)+'\n'
    elif dashUp and not dashDown:
        st='-'*len(st)+'\n'+st+'\n'
    elif not dashUp and dashDown:
        st+='-'*len(st)+'\n'

    return st

def string_add_dash_line(st,):

    st+='-'*len(st.split('\n')[1])+'\n'

    return st

if 1:
    crosslinesHeaderKey='Line, Boundary, Value'
    parametersHeaderKey='Parameter, Axis, Boundary, Value'
    crosslinesKey='%s, %s, %.3e'
    parametersKey='%s, %s, %s, %.3f'

    lst=[
    ('Centroid', 'X', 'section', 246.31952920482601), 
    ('Centroid', 'X', 'under-water-line', 0.34080500000000002), 
    ('Centroid', 'Y', 'section', 18026.300431234307), 
    ('Centroid', 'Y', 'under-water-line', 55924.037961711911), 
    ('Moment I', 'X', 'section', 268549.26328172954), 
    ('Moment I', 'X', 'under-water-line', 1243239.1468088273), 
    ('Moment I', 'Y', 'section', 870.23365699999999), 
    ('Moment I', 'Y', 'under-water-line', 1780.9078169999998), 
    ('Moment II', 'X', 'section', 54.80712793964701), 
    ('Moment II', 'X', 'under-water-line', 49.460183476832832), 
    ('Moment II', 'Y', 'section', 0.0069228618808385165), 
    ('Moment II', 'Y', 'under-water-line', 0.0082489999999999994), 
    ('Moment II', 'XY', 'section', 47648.175716628961), 
    ('Moment II', 'XY', 'under-water-line', 88021.62478363933), 
    ('Centroid Moment II', 'X', 'section', 6.020399342164886), 
    ('Centroid Moment II', 'X', 'under-water-line', 0.0035209999999999998), 
    ('Centroid Moment II', 'Y', 'section', 18026.342138067019), 
    ('Centroid Moment II', 'Y', 'under-water-line', 55924.159145367004), 
    ('Centroid Moment II', 'XY', 'section', 2882575.6346526803), 
    ('Centroid Moment II', 'XY', 'under-water-line', 5599891.3026070464)]

    annotationKey='SECTION: X=1e5\n'

    st=annotationKey

    st+=string_line_spacing(parametersHeaderKey,None)
    for tup in lst:
        st+=string_line_spacing(parametersKey,tup,True,False,False)
    st=string_add_dash_line(st)
    print sys.getdefaultencoding()
    print st
    open(PATH+'test.txt','w').write(st)

From notepad++ while running %CMD64% /k " %python_27a_CMD_settings% && %python_27a% $(FULL_CURRENT_PATH)" im getting text with nice formatted tabs:

SECTION: X=1e5
----------------------------------------------------------------------------------------------------------------------------
 | Parameter                   |  Axis                       |  Boundary                   |  Value                      |
----------------------------------------------------------------------------------------------------------------------------
 | Centroid                    |  X                          |  section                    |  246.320                    |
 | Centroid                    |  X                          |  under-water-line           |  0.341                      |
 | Centroid                    |  Y                          |  section                    |  18026.300                  |
 | Centroid                    |  Y                          |  under-water-line           |  55924.038                  |
 | Moment I                    |  X                          |  section                    |  268549.263                 |
 | Moment I                    |  X                          |  under-water-line           |  1243239.147                |
 | Moment I                    |  Y                          |  section                    |  870.234                    |
 | Moment I                    |  Y                          |  under-water-line           |  1780.908                   |
 | Moment II                   |  X                          |  section                    |  54.807                     |
 | Moment II                   |  X                          |  under-water-line           |  49.460                     |
 | Moment II                   |  Y                          |  section                    |  0.007                      |
 | Moment II                   |  Y                          |  under-water-line           |  0.008                      |
 | Moment II                   |  XY                         |  section                    |  47648.176                  |
 | Moment II                   |  XY                         |  under-water-line           |  88021.625                  |
 | Centroid Moment II          |  X                          |  section                    |  6.020                      |
 | Centroid Moment II          |  X                          |  under-water-line           |  0.004                      |
 | Centroid Moment II          |  Y                          |  section                    |  18026.342                  |
 | Centroid Moment II          |  Y                          |  under-water-line           |  55924.159                  |
 | Centroid Moment II          |  XY                         |  section                    |  2882575.635                |
 | Centroid Moment II          |  XY                         |  under-water-line           |  5599891.303                |
----------------------------------------------------------------------------------------------------------------------------

But in saved file, matplotlib savefig, and console from nppexec using command :

npp_save
cd $(FULL_CURRENT_PATH)
$(SYS.python_27a_CMD_settings) && $(SYS.python_27a) $(FULL_CURRENT_PATH)

its get ugly:

ugly formatted tabs

But when I copy paste it in here its getting formatted properly...

Problem lay in encoding? How to fix it?

3
  • 1
    How does it get ugly? Your ``ugly'' version does not look ugly to me. Commented Jun 7, 2016 at 16:39
  • 4
    First, figure out how to use a fixed width font. Next, find out how you can adjust how many spaces equals a tab in matplotlib Commented Jun 7, 2016 at 16:54
  • 1
    this fixed my issue in matplotlib: stackoverflow.com/questions/22664091/matplotlib-text-alignment Commented Jun 7, 2016 at 17:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.