Python Forum
Open and read a tab delimited file from html using python cgi
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Open and read a tab delimited file from html using python cgi
#1
I am trying build a webpage which takes a large tab delimited .txt/.txt.gz file as user input from a form and using POST method(test.html) to send the data to cgi-bin directory to file.py which ideally should open the file read and put the data into a dataframe and do some analysis(which i have already wrttien in python and works well on terminal) on the data and send back the analysis results to a html page. That is where the problem i am facing, how to read the data in the file exactly with the separator. How to pass these data into a pandas dataframe with delimiter?

test.html

<form enctype = "multipart/form-data" action = "/cgi-bin/file.py" method = "post">
<p>File: <input type = "file" name = "filename" /></p>
<p><input type = "submit" value = "Upload" /></p>
</form>


file.py

import cgi, os
import cgitb; cgitb.enable()
import pandas as pd

form = cgi.FieldStorage()

fileitem = form['filename']
if fileitem.file:
    message=fileitem.file.read()

#df = pd.read_csv(message, sep='\t')
#some code for analysis
#some code for analysis
#some code for analysis
The 'message' if printed, prints the data without any separator in a single line, this should ideally passed into a pandas dataframe.

Thanks in advance for your time
Reply
#2
You should not use CGI anymore.
Python doc Wrote:CGI is going to be deprecated in Python 3.8 and removed in 3.10
The modern and better way is to use eg Flask.

Here a couple of Thread you can look at.
Read CSV data into Pandas DataSet From Variable Flask?
Show csv file in flask template.html
Reply
#3
@snippsat Thank you for your response and suggestion. I am a bioinformatician and a beginner in python and i did try Flask, it was bit of complex for me hence decided to do with CGI.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Python Obstacles | Kung-Fu | Full File HTML Document Scrape and Store it in MariaDB BrandonKastning 5 2,880 Dec-29-2021, 02:26 AM
Last Post: BrandonKastning
  show csv file in flask template.html rr28rizal 8 34,728 Apr-12-2021, 09:24 AM
Last Post: adamabusamra
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,619 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,359 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  Read owl file using python flask Gayathri 1 2,445 Nov-20-2019, 12:56 PM
Last Post: ChislaineWijdeven
  Sending file html ? JohnnyCoffee 3 64,851 Sep-06-2019, 04:32 PM
Last Post: snippsat
  Open unuploaded file bhojendra 1 2,230 Jul-19-2019, 04:10 AM
Last Post: micseydel
  Read XML-File yuyu 16 7,118 Dec-15-2018, 10:49 PM
Last Post: snippsat
  Reading a html file peterl 4 4,539 Aug-20-2018, 03:16 PM
Last Post: peterl
  Open Sqlite database file directly in django sourabhjaiswal92 0 2,947 May-22-2018, 05:55 AM
Last Post: sourabhjaiswal92

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020