Connecting Tech Pros Worldwide Forums | Help | Site Map

Imported tab delimited data into HTML using python

Newbie
 
Join Date: Feb 2007
Posts: 5
#1: Feb 22 '07
Hi,

I'm creating a HTML report using Python and would like to import some tabular data which is in tab delimited format into the HTML page.

The data needs to be displayed as a table within the HTML page, but i'm not sure how to go about doing it.

Thanks in advance,

Niten

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#2: Feb 22 '07

re: Imported tab delimited data into HTML using python


Quote:

Originally Posted by nitenmistry

Hi,

I'm creating a HTML report using Python and would like to import some tabular data which is in tab delimited format into the HTML page.

The data needs to be displayed as a table within the HTML page, but i'm not sure how to go about doing it.

Thanks in advance,

Niten

It seems this is a combined HTML and Python question. Using Python to insert formatted data into an existing file is straightforward if we know where to put it (line number, after a certain keyword or character sequence, etc.). Since I don't know HTML (and don't really want to know), I connot help with that. We may have an expert here that knows both, or someone on the HTML forum can help.
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#3: Feb 22 '07

re: Imported tab delimited data into HTML using python


Quote:

Originally Posted by nitenmistry

Hi,

I'm creating a HTML report using Python and would like to import some tabular data which is in tab delimited format into the HTML page.

The data needs to be displayed as a table within the HTML page, but i'm not sure how to go about doing it.

Thanks in advance,

Niten


Does the HTML page exist already, or are you creating an HTML page to display the data?
The way I would do this, is to parse through your tab delimited data set, and store the data as a list of lists.
Then you can iterate through with nested for loops writing out HTML tags.
Newbie
 
Join Date: Feb 2007
Posts: 5
#4: Feb 22 '07

re: Imported tab delimited data into HTML using python


Quote:

Originally Posted by Motoma

Does the HTML page exist already, or are you creating an HTML page to display the data?
The way I would do this, is to parse through your tab delimited data set, and store the data as a list of lists.
Then you can iterate through with nested for loops writing out HTML tags.

I'm creating a HTML page which needs to display data which is in the tab delimited format.

I've only just started learning Python, how would I go about parsing through the delimited data set?

Thanks again,

Niten
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#5: Feb 22 '07

re: Imported tab delimited data into HTML using python


Quote:

Originally Posted by nitenmistry

I'm creating a HTML page which needs to display data which is in the tab delimited format.

I've only just started learning Python, how would I go about parsing through the delimited data set?

Thanks again,

Niten

How much of the code have you done so far?
Newbie
 
Join Date: Feb 2007
Posts: 5
#6: Feb 22 '07

re: Imported tab delimited data into HTML using python


Quote:

Originally Posted by Motoma

How much of the code have you done so far?

Not much, i'm trying to build on something which I have done previously so that variables from a text file can be passed into a script.
Expand|Select|Wrap|Line Numbers
  1. def openAndParse(inpName):
  2.  
  3.     inp=open(inpName)
  4.     lines = inp.readlines()
  5.     inp.close()
  6.  
  7.     params = lines[6:62]
  8.     for p in params:
  9.         exec(p[6:])
  10.  
  11.     return h5
  12.  
Im not sure how to iterate through the nested loops and attach HTML tags

Regards,

Niten
dshimer's Avatar
Expert
 
Join Date: Dec 2006
Location: Central Ohio, USA
Posts: 135
#7: Feb 22 '07

re: Imported tab delimited data into HTML using python


The parsing is pretty straightforward. Looking at one line that has mixed text but is delimited by tabs.
Expand|Select|Wrap|Line Numbers
  1. >>> txt='this and\tthat\twith some\tbeside'
  2. >>> txt.split('\t')
  3. ['this and', 'that', 'with some', 'beside']
  4.  
note that by specifying the split only occur at the tabs, strings that have a space between will stay together. split can take any delimiter, or if none is specified then any whitespace will cause a split.

readlines will return the whole file as a list of individual lines, so for each line in the list of data lines split at the tabs and append the result to a new list. In the end you will have a list with the same number of elements as the file had lines, each element will be a list of parsed data. For example
Expand|Select|Wrap|Line Numbers
  1. >>> file=open('/tmp/tmp.txt','r')
  2. >>> lines=file.readlines()
  3. >>> fulllist=[]
  4. >>> for line in lines:
  5. ...     fulllist.append(line.split('\t'))
  6. ...     
  7. >>> fulllist
  8. [['a', '3', 'b', '4\n'], ['c', '5', 'd', '6\n'], ['four', 'words', 'with', 'tabs\n']]
  9. >>> 
by changing line.split('\t') to line.replace('\n','').split('\t') you can also strip the newline character before the split.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#8: Feb 22 '07

re: Imported tab delimited data into HTML using python


Quote:

Originally Posted by dshimer

The parsing is pretty straightforward. Looking at one line that has mixed text but is delimited by tabs.

Expand|Select|Wrap|Line Numbers
  1. >>> txt='this and\tthat\twith some\tbeside'
  2. >>> txt.split('\t')
  3. ['this and', 'that', 'with some', 'beside']
  4.  
note that by specifying the split only occur at the tabs, strings that have a space between will stay together. split can take any delimiter, or if none is specified then any whitespace will cause a split.

readlines will return the whole file as a list of individual lines, so for each line in the list of data lines split at the tabs and append the result to a new list. In the end you will have a list with the same number of elements as the file had lines, each element will be a list of parsed data. For example
Expand|Select|Wrap|Line Numbers
  1. >>> file=open('/tmp/tmp.txt','r')
  2. >>> lines=file.readlines()
  3. >>> fulllist=[]
  4. >>> for line in lines:
  5. ...     fulllist.append(line.split('\t'))
  6. ...     
  7. >>> fulllist
  8. [['a', '3', 'b', '4\n'], ['c', '5', 'd', '6\n'], ['four', 'words', 'with', 'tabs\n']]
  9. >>> 
by changing line.split('\t') to line.replace('\n','').split('\t') you can also strip the newline character before the split.

This will remove leading and trailing whitespace characters:
Expand|Select|Wrap|Line Numbers
  1. s.strip().split('\t')
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#9: Feb 22 '07

re: Imported tab delimited data into HTML using python


I needed to parse some tab delimited files recently. I hope this will help you. Keep in mind I do not know HTML.
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. fn = 'H:/TEMP/temsys/MemData.txt'
  4.  
  5. f = open(fn, 'r')
  6. labelLst = f.readline().strip().split('\t')
  7. lineLst = []
  8.  
  9. for line in f:
  10.     if not line.startswith('#'):
  11.         lineLst.append(line.strip().split('\t'))
  12.  
  13. s1 = '%s\n%s\n' % ('<HTML tag>', ', '.join(labelLst))
  14. sLst = []
  15. for line in lineLst:
  16.     sLst.append(','.join(line)+'\n')
  17.  
  18. s2 = ''.join(sLst)
  19.  
  20. finished_string = '%s%s%s' % (s1, s2, '</HTML tag>')
  21. print finished_string
  22.  
  23.  
  24. """ file data
  25. mem_no    mod_azimuth    vessel_OR    platform_IR    platform_OR    toe_dir    brkt_type    tos_el    hr_ext
  26. ##############################################################################################################
  27. 831    90.0    109.0    120.0    216.0    In    F    1456.5    Yes
  28. 832    337.0    109.0    120.0    216.0    In    F1    1456.5    Yes
  29. 833    316.0    109.0    120.0    216.0    In    F2    1456.5    Yes
  30. 834    298.0    109.0    120.0    192.0    Out    F3    1456.5    Yes
  31. 836    277.0    109.0    120.0    192.0    Out    F4    1456.5    Yes
  32. 837    270.0    109.0    120.0    192.0    In    F    1468.5    Yes
  33. 838    256.0    109.0    120.0    192.0    In    F    1468.5    No
  34. 839    180.0    109.0    120.0    216.0    Out    F2    1456.5    Yes
  35. 840    59.0    109.0    120.0    216.0    In    F    1456.5    Yes
  36. 841    39.0    109.0    120.0    216.0    In    F    1456.5    Yes
  37. 842    17.0    109.0    120.0    216.0    Out    F    1456.5    Yes
  38. 849    356.0    109.0    120.0    216.0    Out    F    1456.5    Yes
  39. """
  40. """ finished_string
  41. <HTML tag>
  42. mem_no, mod_azimuth, vessel_OR, platform_IR, platform_OR, toe_dir, brkt_type, tos_el, hr_ext
  43. 831,90.0,109.0,120.0,216.0,In,F,1456.5,Yes
  44. 832,337.0,109.0,120.0,216.0,In,F1,1456.5,Yes
  45. 833,316.0,109.0,120.0,216.0,In,F2,1456.5,Yes
  46. 834,298.0,109.0,120.0,192.0,Out,F3,1456.5,Yes
  47. 836,277.0,109.0,120.0,192.0,Out,F4,1456.5,Yes
  48. 837,270.0,109.0,120.0,192.0,In,F,1468.5,Yes
  49. 838,256.0,109.0,120.0,192.0,In,F,1468.5,No
  50. 839,180.0,109.0,120.0,216.0,Out,F2,1456.5,Yes
  51. 840,59.0,109.0,120.0,216.0,In,F,1456.5,Yes
  52. 841,39.0,109.0,120.0,216.0,In,F,1456.5,Yes
  53. 842,17.0,109.0,120.0,216.0,Out,F,1456.5,Yes
  54. 849,356.0,109.0,120.0,216.0,Out,F,1456.5,Yes
  55. </HTML tag>
  56. >>>
  57. """
You will need to format the data to suit the way you want to display the information - probably columnar - so instead of a comma, you would use a pad string that needs to be calculated. I recall that Barton posted a nifty function for creating pad strings. I will try to find it.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#10: Feb 22 '07

re: Imported tab delimited data into HTML using python


I found Barton's function and applied it to the example:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. col_width = 14
  4.  
  5. # Barton wrote this
  6. def columnize(word, width):
  7.     nSpaces = width - len(word)
  8.     if nSpaces < 0:
  9.         nSpaces = 0
  10.     return word + (" " * nSpaces)
  11.  
  12. fn = 'H:/TEMP/temsys/MemData.txt'
  13.  
  14. f = open(fn, 'r')
  15. labelLst = f.readline().strip().split('\t')
  16. lineLst = []
  17.  
  18. for line in f:
  19.     if not line.startswith('#'):
  20.         lineLst.append(line.strip().split('\t'))
  21.  
  22. s1 = '<HTML tag>\n'
  23.  
  24. for word in labelLst:
  25.     s1 += columnize(word, col_width)
  26.  
  27. sLst = []
  28. for line in lineLst:
  29.     line_of_words = ''
  30.     for word in line:
  31.         line_of_words += columnize(word, col_width)
  32.     sLst.append(line_of_words+'\n')
  33.  
  34. s2 = ''.join(sLst)
  35.  
  36. finished_string = '%s\n%s\n%s%s' % (s1, '='*col_width*len(labelLst), s2, '</HTML tag>')
  37. print finished_string
  38.  
  39. """ finished_string
  40. >>> <HTML tag>
  41. mem_no        mod_azimuth   vessel_OR     platform_IR   platform_OR   toe_dir       brkt_type     tos_el        hr_ext        
  42. ==============================================================================================================================
  43. 831           90.0          109.0         120.0         216.0         In            F             1456.5        Yes           
  44. 832           337.0         109.0         120.0         216.0         In            F1            1456.5        Yes           
  45. 833           316.0         109.0         120.0         216.0         In            F2            1456.5        Yes           
  46. 834           298.0         109.0         120.0         192.0         Out           F3            1456.5        Yes           
  47. 836           277.0         109.0         120.0         192.0         Out           F4            1456.5        Yes           
  48. 837           270.0         109.0         120.0         192.0         In            F             1468.5        Yes           
  49. 838           256.0         109.0         120.0         192.0         In            F             1468.5        No            
  50. 839           180.0         109.0         120.0         216.0         Out           F2            1456.5        Yes           
  51. 840           59.0          109.0         120.0         216.0         In            F             1456.5        Yes           
  52. 841           39.0          109.0         120.0         216.0         In            F             1456.5        Yes           
  53. 842           17.0          109.0         120.0         216.0         Out           F             1456.5        Yes           
  54. 849           356.0         109.0         120.0         216.0         Out           F             1456.5        Yes           
  55. </HTML tag>
  56. >>>
  57. """
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#11: Feb 22 '07

re: Imported tab delimited data into HTML using python


The original problem was to format this into an HTML Table.

The general format for this is:
Expand|Select|Wrap|Line Numbers
  1. <table>
  2.     <tr><th>header column 1</th><th>header column 2</th><th>header column 3</th></tr>
  3.     <tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td><td>Row 1 Col 3</td></tr>
  4.     <tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td><td>Row 2 Col 3</td></tr>
  5.     <tr><td>Row 3 Col 1</td><td>Row 3 Col 2</td><td>Row 3 Col 3</td></tr>
  6. </table>
  7.  
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#12: Feb 23 '07

re: Imported tab delimited data into HTML using python


Quote:

Originally Posted by Motoma

The original problem was to format this into an HTML Table.

The general format for this is:

Expand|Select|Wrap|Line Numbers
  1. <table>
  2.     <tr><th>header column 1</th><th>header column 2</th><th>header column 3</th></tr>
  3.     <tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td><td>Row 1 Col 3</td></tr>
  4.     <tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td><td>Row 2 Col 3</td></tr>
  5.     <tr><td>Row 3 Col 1</td><td>Row 3 Col 2</td><td>Row 3 Col 3</td></tr>
  6. </table>
  7.  

Thanks Motoma!
Expand|Select|Wrap|Line Numbers
  1. """
  2. Read a tab delimited file and format for an HTML table
  3. The general format for an HTML table:
  4. <table>
  5.     <tr><th>header column 1</th><th>header column 2</th><th>header column 3</th></tr>
  6.     <tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td><td>Row 1 Col 3</td></tr>
  7.     <tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td><td>Row 2 Col 3</td></tr>
  8.     <tr><td>Row 3 Col 1</td><td>Row 3 Col 2</td><td>Row 3 Col 3</td></tr>
  9. </table>
  10. """
  11. fn = 'H:/TEMP/temsys/MemData.txt'
  12.  
  13. f = open(fn, 'r')
  14. labelLst = f.readline().strip().split('\t')
  15. lineLst = []
  16.  
  17. for line in f:
  18.     if not line.startswith('#'):
  19.         lineLst.append(line.strip().split('\t'))
  20.  
  21. s1 = '<table>\n'
  22. s1a = '%s%s%s%s' % ('    <tr><th>','</th><th>'.join(labelLst),'</td></tr>','\n')
  23.  
  24. sLst = []
  25. for line in lineLst:
  26.     line_of_words = '%s%s%s%s' % ('    <tr><th>','</th><th>'.join(line),'</td></tr>','\n')
  27.     sLst.append(line_of_words)
  28.  
  29. s2 = ''.join(sLst)
  30.  
  31. finished_string = '%s%s%s%s' % (s1, s1a, s2, '</table>')
  32. print finished_string
  33.  
  34. """ data file
  35. mem_no    mod_azimuth    vessel_OR    platform_IR    platform_OR    toe_dir    brkt_type    tos_el    hr_ext
  36. ##############################################################################################################
  37. 831    90.0    109.0    120.0    216.0    In    F    1456.5    Yes
  38. 832    337.0    109.0    120.0    216.0    In    F1    1456.5    Yes
  39. 833    316.0    109.0    120.0    216.0    In    F2    1456.5    Yes
  40. 834    298.0    109.0    120.0    192.0    Out    F3    1456.5    Yes
  41. 836    277.0    109.0    120.0    192.0    Out    F4    1456.5    Yes
  42. 837    270.0    109.0    120.0    192.0    In    F    1468.5    Yes
  43. 838    256.0    109.0    120.0    192.0    In    F    1468.5    No
  44. 839    180.0    109.0    120.0    216.0    Out    F2    1456.5    Yes
  45. 840    59.0    109.0    120.0    216.0    In    F    1456.5    Yes
  46. 841    39.0    109.0    120.0    216.0    In    F    1456.5    Yes
  47. 842    17.0    109.0    120.0    216.0    Out    F    1456.5    Yes
  48. 849    356.0    109.0    120.0    216.0    Out    F    1456.5    Yes
  49. """
  50.  
  51. """ finished_string
  52. >>>
  53. <table>
  54.     <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>
  55.     <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>
  56.     <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>
  57.     <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>
  58.     <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>
  59.     <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>
  60.     <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>
  61.     <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>
  62.     <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>
  63.     <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>
  64.     <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>
  65.     <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>
  66.     <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>
  67. </table>
  68. >>>
  69. """
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#13: Feb 23 '07

re: Imported tab delimited data into HTML using python


Quote:

Originally Posted by bvdet

Thanks Motoma!

Glad to help.
Newbie
 
Join Date: Feb 2007
Posts: 5
#14: Feb 23 '07

re: Imported tab delimited data into HTML using python


Thanks everyone, you've been a great help

Regards,

Niten
Reply