CSV | Python In HTML Examples https://pythoninhtmlexamples.com Showing You How You Can Use Python On Your Website Wed, 11 Jan 2023 21:52:12 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 194754043 How To Read A CSV Into A HTML Table With Python https://pythoninhtmlexamples.com/how-to-read-a-csv-into-a-html-table-with-python/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-read-a-csv-into-a-html-table-with-python Wed, 11 Jan 2023 21:16:02 +0000 https://pythoninhtmlexamples.com/?p=350 A lot can been done with a CSV (“comma separated value”) file. If you can run a web scraping script on your local machine, you can FTP the results to a FTP site in the ubiquitous CSV format, and then render it on your web page with HTML. The rest of this post demonstrates a […]

The post How To Read A CSV Into A HTML Table With Python first appeared on Python In HTML Examples.]]>
A lot can been done with a CSV (“comma separated value”) file.

If you can run a web scraping script on your local machine, you can FTP the results to a FTP site in the ubiquitous CSV format, and then render it on your web page with HTML.

The rest of this post demonstrates a basic example of how you can read a CSV file into an HTML table using Python.

Add an iframe to your page and point it to your python (py) file:

Here is the code for the file:


#!/usr/bin/python
#import csv  #the module is already present so no need to add it again


#MAKE SURE THE FILE PERMISSIONS ARE 755!!!!

filename = open('file_to_ftp.csv') 


# print HTTP header
print "Content-type: text/html\n\n"

# define an HTML template
print "<!DOCTYPE html>"
print "<html>"
print "<head>"
print "<title>CSVParse</title>"
print "</head>"
print "<body>"

print("<table style='width:100%'>")

print("<tr>")
print("<th>Index</th>")
print("<th>Company</th>")
print("<th>Link</th>")
print("<th>Header</th>")

print("</tr>")

for line in filename:	
    row = line.split(",")
    print("<tr>")
    print ("<td>" + str(row[0]) + "</td>")
    print ("<td>" + str(row[1]) + "</td>")
    print ("<td>" + str(row[2]) + "</td>")
    print ("<td>" + str(row[3]) + "</td>")
    print ("</tr>")


print("</table>")


print "</body>"
print "</html>"


Click here for the file in action.

Let me know if you need assistance.

FacebooktwitterredditpinterestlinkedinmailThe post How To Read A CSV Into A HTML Table With Python first appeared on Python In HTML Examples.]]>
350