0

Am having a text file where i read the content and assigning it to a variable. I need to put the contents in a HTML table. As of now, i wrote a python snippet to put the contents in a variable. How to convert it to a HTML table. I need to loop through the file and assign the variable and update the data as a field value in a table. How can i do it ? Help needed

My .txt file is

34,52,l,ma,cgb,eta
45,52,X,,lta,tox
67,52,V,nm,,oyt

My code to read is :

imp_auth = []
asn_eng = []


with open ("FR_list.txt","rt") as f:
 for line in f:
  (id,rel,sta,sev,ia,ae,iam,aem) = line.split(',')
  imp_auth.append(iam)
  asn_eng.append(aem)

3 Answers 3

1

You could use some of the available html templating engines and render the template.

For example, by using Jinja2 tempting engine

from jinja2 import Template
table = """
<table>
    {% for line in lines %}
    <tr>
        <td>{{ line[0] }}</td>
        <td>{{ line[1] }}</td>
        <td>{{ line[2] }}</td>
        ...
    </tr>
    {% endfor %}
</table>
"""
with open("FR_list.txt", "rt") as f:
    template = Template(table)
    t = template.render(lines=[line.split(',') for line in f])
    print(t)
    """
    <table>
        <tr>
            <td>34</td>
            <td>52</td>
            <td>l</td>
            ...
        </tr>
        ...
    </table>
    """
Sign up to request clarification or add additional context in comments.

Comments

1

I would separate the things you're trying to do. Here's how I would do it (when not using some framework of some sort):

def parse(filename='content.txt'):
    with open(filename, 'r') as fh:
        for line in fh.readlines():
            row = [x.strip() for x in line.split(',')]
            if len(row) < 2: continue
            yield row[-2:] # iam,aem

def format_element(element):
    return '\n    <td>{elem}</td>'.format(elem=element)

def format_row(row): 
    return  '<tr>' + ''.join([format_element(element) for element in row]) + '\n</tr>'

with open('result.html', 'w') as output:
    for row in parse():    
        output.write(format_row(row))
        print format_row(row)

Here's the output I got:

<tr>
    <td>cgb</td>
    <td>eta</td>
</tr>
<tr>
    <td>lta</td>
    <td>tox</td>
</tr>
<tr>
    <td></td>
    <td>oyt</td>
</tr>

Comments

0
You can use the following
imp_auth = []
with open ("tt.txt","rt") as f:
    for line in f:
        (id,rel,sta,sev,iam,aem) = line.split(',')
        imp_auth.append((id,rel,sta,sev,iam,aem.replace('\n','')))
result = '<table>'
result += '<th>col1</th><th>col2</th><th>col3</th>'
result += '<th>col4</th><th>col5</th><th>col6</th>'
for t in range(0,len(imp_auth)):
    for ele in imp_auth[t]:        
        result += '<tr>'
        result += '<td>ele[0]</td>ele[1]<td>ele[2]</td>'
        result += '<td>ele[3]</td>ele[4]<td>ele[5]</td>'
        result += '</tr>'

result += '</table>'    

Comments

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.