Back to SDS/2 Parametric Scripts

 

### Reformat tab delimited data

"""

Read a tab delimited file and format comma delimited

"""

 

fn = 'H:/TEMP/temsys/MemData.txt'

 

f = open(fn, 'r')

labelLst = f.readline().strip().split('\t')

lineLst = []

 

for line in f:

    if not line.startswith('#'):

        lineLst.append(line.strip().split('\t'))

 

print labelLst

print lineLst

 

s1 = '%s\n%s\n' % ('<tag>', ', '.join(labelLst))

sLst = []

for line in lineLst:

    sLst.append(','.join(line)+'\n')

   

s2 = ''.join(sLst)

   

finished_string = '%s%s%s' % (s1, s2, '</tag>')

print finished_string

 

 

"""

mem_no      mod_azimuth vessel_OR   platform_IR platform_OR toe_dir     brkt_type   tos_el      hr_ext

##############################################################################################################

831   90.0  109.0 120.0 216.0 In    F     1456.5      Yes

832   337.0 109.0 120.0 216.0 In    F1    1456.5      Yes

833   316.0 109.0 120.0 216.0 In    F2    1456.5      Yes

834   298.0 109.0 120.0 192.0 Out   F3    1456.5      Yes

836   277.0 109.0 120.0 192.0 Out   F4    1456.5      Yes

837   270.0 109.0 120.0 192.0 In    F     1468.5      Yes

838   256.0 109.0 120.0 192.0 In    F     1468.5      No

839   180.0 109.0 120.0 216.0 Out   F2    1456.5      Yes

840   59.0  109.0 120.0 216.0 In    F     1456.5      Yes

841   39.0  109.0 120.0 216.0 In    F     1456.5      Yes

842   17.0  109.0 120.0 216.0 Out   F     1456.5      Yes

849   356.0 109.0 120.0 216.0 Out   F     1456.5      Yes

"""

"""

<tag>

mem_no, mod_azimuth, vessel_OR, platform_IR, platform_OR, toe_dir, brkt_type, tos_el, hr_ext

831,90.0,109.0,120.0,216.0,In,F,1456.5,Yes

832,337.0,109.0,120.0,216.0,In,F1,1456.5,Yes

833,316.0,109.0,120.0,216.0,In,F2,1456.5,Yes

834,298.0,109.0,120.0,192.0,Out,F3,1456.5,Yes

836,277.0,109.0,120.0,192.0,Out,F4,1456.5,Yes

837,270.0,109.0,120.0,192.0,In,F,1468.5,Yes

838,256.0,109.0,120.0,192.0,In,F,1468.5,No

839,180.0,109.0,120.0,216.0,Out,F2,1456.5,Yes

840,59.0,109.0,120.0,216.0,In,F,1456.5,Yes

841,39.0,109.0,120.0,216.0,In,F,1456.5,Yes

842,17.0,109.0,120.0,216.0,Out,F,1456.5,Yes

849,356.0,109.0,120.0,216.0,Out,F,1456.5,Yes

</tag>

>>>

"""

 

"""

Read a tab delimited file and format into aligned rows and columns

"""

 

col_width = 14

 

# Barton wrote this

def columnize(word, width):

    nSpaces = width - len(word)

    if nSpaces < 0:

        nSpaces = 0

    return word + (" " * nSpaces)

 

fn = 'H:/TEMP/temsys/MemData.txt'

 

f = open(fn, 'r')

labelLst = f.readline().strip().split('\t')

lineLst = []

 

for line in f:

    if not line.startswith('#'):

        lineLst.append(line.strip().split('\t'))

 

s1 = '<tag>\n'

 

for word in labelLst:

    s1 += columnize(word, col_width)

   

sLst = []

for line in lineLst:

    line_of_words = ''

    for word in line:

        line_of_words += columnize(word, col_width)

    sLst.append(line_of_words+'\n')

       

s2 = ''.join(sLst)

 

finished_string = '%s\n%s\n%s%s' % (s1, '='*col_width*len(labelLst), s2, '</tag>')

print finished_string

 

 

""" data file

mem_no      mod_azimuth vessel_OR   platform_IR platform_OR toe_dir     brkt_type   tos_el      hr_ext

##############################################################################################################

831   90.0  109.0 120.0 216.0 In    F     1456.5      Yes

832   337.0 109.0 120.0 216.0 In    F1    1456.5      Yes

833   316.0 109.0 120.0 216.0 In    F2    1456.5      Yes

834   298.0 109.0 120.0 192.0 Out   F3    1456.5      Yes

836   277.0 109.0 120.0 192.0 Out   F4    1456.5      Yes

837   270.0 109.0 120.0 192.0 In    F     1468.5      Yes

838   256.0 109.0 120.0 192.0 In    F     1468.5      No

839   180.0 109.0 120.0 216.0 Out   F2    1456.5      Yes

840   59.0  109.0 120.0 216.0 In    F     1456.5      Yes

841   39.0  109.0 120.0 216.0 In    F     1456.5      Yes

842   17.0  109.0 120.0 216.0 Out   F     1456.5      Yes

849   356.0 109.0 120.0 216.0 Out   F     1456.5      Yes

"""

 

""" finished_string

>>> <tag>

mem_no        mod_azimuth   vessel_OR     platform_IR   platform_OR   toe_dir       brkt_type     tos_el        hr_ext       

==============================================================================================================================

831           90.0          109.0         120.0         216.0         In            F             1456.5        Yes          

832           337.0         109.0         120.0         216.0         In            F1            1456.5        Yes          

833           316.0         109.0         120.0         216.0         In            F2            1456.5        Yes          

834           298.0         109.0         120.0         192.0         Out           F3            1456.5        Yes          

836           277.0         109.0         120.0         192.0         Out           F4            1456.5        Yes          

837           270.0         109.0         120.0         192.0         In            F             1468.5        Yes          

838           256.0         109.0         120.0         192.0         In            F             1468.5        No           

839           180.0         109.0         120.0         216.0         Out           F2            1456.5        Yes          

840           59.0          109.0         120.0         216.0         In            F             1456.5        Yes          

841           39.0          109.0         120.0         216.0         In            F             1456.5        Yes          

842           17.0          109.0         120.0         216.0         Out           F             1456.5        Yes          

849           356.0         109.0         120.0         216.0         Out           F             1456.5        Yes          

</tag>

>>>

"""

 

"""

Read a tab delimited file and format for an HTML table

The general format for an HTML table:

<table>

    <tr><th>header column 1</th><th>header column 2</th><th>header column 3</th></tr>

    <tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td><td>Row 1 Col 3</td></tr>

    <tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td><td>Row 2 Col 3</td></tr>

    <tr><td>Row 3 Col 1</td><td>Row 3 Col 2</td><td>Row 3 Col 3</td></tr>

</table>

"""

 

col_width = 14

 

fn = 'H:/TEMP/temsys/MemData.txt'

 

f = open(fn, 'r')

labelLst = f.readline().strip().split('\t')

lineLst = []

 

for line in f:

    if not line.startswith('#'):

        lineLst.append(line.strip().split('\t'))

 

s1 = '<table>\n'

s1a = '%s%s%s%s' % ('    <tr><th>','</th><th>'.join(labelLst),'</td></tr>','\n')

    

sLst = []

for line in lineLst:

    line_of_words = '%s%s%s%s' % ('    <tr><th>','</th><th>'.join(line),'</td></tr>','\n')

    sLst.append(line_of_words)

       

s2 = ''.join(sLst)

 

finished_string = '%s%s%s%s' % (s1, s1a, s2, '</table>')

print finished_string

 

 

""" data file

mem_no      mod_azimuth vessel_OR   platform_IR platform_OR toe_dir     brkt_type   tos_el      hr_ext

##############################################################################################################

831   90.0  109.0 120.0 216.0 In    F     1456.5      Yes

832   337.0 109.0 120.0 216.0 In    F1    1456.5      Yes

833   316.0 109.0 120.0 216.0 In    F2    1456.5      Yes

834   298.0 109.0 120.0 192.0 Out   F3    1456.5      Yes

836   277.0 109.0 120.0 192.0 Out   F4    1456.5      Yes

837   270.0 109.0 120.0 192.0 In    F     1468.5      Yes

838   256.0 109.0 120.0 192.0 In    F     1468.5      No

839   180.0 109.0 120.0 216.0 Out   F2    1456.5      Yes

840   59.0  109.0 120.0 216.0 In    F     1456.5      Yes

841   39.0  109.0 120.0 216.0 In    F     1456.5      Yes

842   17.0  109.0 120.0 216.0 Out   F     1456.5      Yes

849   356.0 109.0 120.0 216.0 Out   F     1456.5      Yes

"""

 

""" finished_string

>>>

<table>

    <tr><th>mem_no</th><th>mod_azimuth</th><th>vessel_OR</th><th>platform_IR</th><th>platform_OR</th><th>toe_dir</th><th>brkt_type</th><th>tos_el</th><th>hr_ext</td></tr>

    <tr><th>831</th><th>90.0</th><th>109.0</th><th>120.0</th><th>216.0</th><th>In</th><th>F</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>832</th><th>337.0</th><th>109.0</th><th>120.0</th><th>216.0</th><th>In</th><th>F1</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>833</th><th>316.0</th><th>109.0</th><th>120.0</th><th>216.0</th><th>In</th><th>F2</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>834</th><th>298.0</th><th>109.0</th><th>120.0</th><th>192.0</th><th>Out</th><th>F3</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>836</th><th>277.0</th><th>109.0</th><th>120.0</th><th>192.0</th><th>Out</th><th>F4</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>837</th><th>270.0</th><th>109.0</th><th>120.0</th><th>192.0</th><th>In</th><th>F</th><th>1468.5</th><th>Yes</td></tr>

    <tr><th>838</th><th>256.0</th><th>109.0</th><th>120.0</th><th>192.0</th><th>In</th><th>F</th><th>1468.5</th><th>No</td></tr>

    <tr><th>839</th><th>180.0</th><th>109.0</th><th>120.0</th><th>216.0</th><th>Out</th><th>F2</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>840</th><th>59.0</th><th>109.0</th><th>120.0</th><th>216.0</th><th>In</th><th>F</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>841</th><th>39.0</th><th>109.0</th><th>120.0</th><th>216.0</th><th>In</th><th>F</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>842</th><th>17.0</th><th>109.0</th><th>120.0</th><th>216.0</th><th>Out</th><th>F</th><th>1456.5</th><th>Yes</td></tr>

    <tr><th>849</th><th>356.0</th><th>109.0</th><th>120.0</th><th>216.0</th><th>Out</th><th>F</th><th>1456.5</th><th>Yes</td></tr>

</table>

>>>

"""