I am trying build a webpage which takes a large tab delimited .txt/.txt.gzfile 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

Recommended Answers

According to the documentation, pd.read_table instead of pd.read_csv might work better.

Jump to Post

All 3 Replies

@tinstaafl, Thank you for your response. However pd.read_table is giving a '500 Internal Server Error '.
Here's what i have tried

#!/usr/bin/python

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_table(message, sep='\t')

print """\
Content-Type: text/html\n
<html>
<body>
   <p>%s</p>
</body>
</html>
""" % (df,)

Thank you

Are you passing the path to the file in this pattern - file://localhost/path/to/table.csv?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.