Open and read a tab delimited file from html using python cgi

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luffy3not2
    New Member
    • Aug 2020
    • 2

    Open and read a tab delimited file from html using python cgi

    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.htm l) 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
    Code:
    <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

    Code:
    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
  • madankarmukta
    Contributor
    • Apr 2008
    • 308

    #2
    Please follow the standalone syntax for read_csv. Moreover engin might not be getting correct fs path. Can you please put here the fs message you are getting ?

    Thanks.

    Comment

    • luffy3not2
      New Member
      • Aug 2020
      • 2

      #3
      @madankarmukta Thank you for your response. fs message is working fine, its suppose open the file and read which its doing fine.

      But this print the entire data in a single line(please see the screenshot), i would like it to put in a table/dataframe.


      Thank you

      PS the image is the 'fs message' which prints the data in the file which is uploaded to test.html

      Comment

      Working...