Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

376
Views
How To Read and Print Data from CSV file into HTML File as Text

I've been trying to read data from .csv file and print the data as text/number into HTML file. Like the below example:

Data from CSV file looks like this

Now, I want to print in HTML like this:

Current age of Mr. abc is 30 Years. The bold & Italic text/numbers will be coming from the csv file and as soon as the csv file updated, the data in HTML will also update. Both the csv and HTML file will be in same local folder.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The usual approach is to use Python's built in CSV reader to correctly parse each row into a list of values. The can be done as follows:

import csv

with open('input.csv') as f_input, open('output.html', 'w') as f_output:
    csv_input = csv.reader(f_input)
    header = next(csv_input)    # skip the header
    
    # Write the HTML header
    f_output.write("<html>\n<body>\n")
    
    for row in csv_input:
        f_output.write(f"Current age of Mr. {row[0]} is {row[1]} Years.<br>\n")
        
    # Write the HTML footer
    f_output.write("</body>\n</html>\n")

For your sample CSV data, this would produce the following HTML output:

<html>
<body>
Current age of Mr. abc is 20 Years.<br>
Current age of Mr. xyz is 30 Years.<br>
Current age of Mr. jkl is 40 Years.<br>
</body>
</html>

To create one HTML per row, you would need to rearrange things a bit and also decide on a naming scheme for the output files. This adds the name to each filename:

import csv

with open('input.csv', encoding='utf-8') as f_input:
    csv_input = csv.reader(f_input)
    header = next(csv_input)    # skip the header

    for row in csv_input:
        with open(f'output_{row[0]}.html', 'w', encoding='utf-8') as f_output:
            # Write the HTML header
            f_output.write("<html>\n<body>\n")

            f_output.write(f"Current age of Mr. {row[0]} is {row[1]} Years.<br>\n")
        
            # Write the HTML footer
            f_output.write("</body>\n</html>\n")

An alternative approach would be to parse each line yourself, this could be done as follows:

with open('input.csv') as f_input, open('output.html', 'w') as f_output:
    header = next(f_input)    # skip the header
    
    # Write the HTML header
    f_output.write("<html>\n<body>\n")
    
    for line in f_input:
        row = line.strip().split(',')
        f_output.write(f"Current age of Mr. {row[0]} is {row[1]} Years.<br>\n")
        
    # Write the HTML footer
    f_output.write("</body>\n</html>\n")
   

This though would not work if your single name column contained commas. e.g. "John, Smith" (which is valid in CSV files, the quotes are used to ignore the commas inside)

about 4 years ago · Juan Pablo Isaza Report

0

The link to the CSV data is not valid, so it is difficult to say, but in general:

  • read the CSV file and split the line into specific variables (see how to read a file, how to split the css, and assign into variables)
  • the you can use the variables in HTML strings, you can use Jinja templates, but I believe that formatting the strings manually will do just fine.

So for example if the data file is data.csv and looks like this:

John:30
Emily:40
Carla:25

you can read it using the with open routine:

with open('data.csv', 'r') as datafile:
    data = datafile.readlines()

Now you have all the data in one list of lines, so you can iterate over the list and parse the data:

newdata = []
for line in data:
    newdata.append(line.rstrip().split(':'))

Now you should have all the couplets in the newdata list and you can use it as you wish. For instance to print out the HTML table, you could do:

print('<table>')
for couple in newdata:
    print(f"<tr><td>{couple[0]}</td><td>{couple[1]}</td></tr>")
print('</table>')

This will print out HTML code for the table. To be able to control the looks of some elements by CSS, you need to use a tag like div or span with some classes, like this:

for couple in newdata:
    print(f"The age of <div class="name">{couple[0]}</div> is <div class="age">{couple[1]}</div>.")

As already said, when the data is parsed, you can use whatever technique do produce the HTML - manual, Jinja templates, ... to save out the HTML file.

about 4 years ago · Juan Pablo Isaza Report

0

Your link data file is not a valid link. I'll still provide you a sample implementation that you can adapt to your case:

sameple.csv

Name, Age
ABC, 30
...

Have a separate script in same folder to create html based on your csv

csv_data=open('sample.csv','r').read().split('\n')
html_body = ''

for line in csv_data:
    line = line.split(',')
    html_body += f'<p>Mr. {line[0]} is {line[1]} Years</p>\n'

html_output = '<html><body>\n'+html_body+'</body></html>'
with open('html_out.html', 'w') as outfile:
outfile.write(html_output)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!