By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,401 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,401 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Expand|Select|Wrap|Line Numbers
  1. <form enctype = "multipart/form-data" action = "/cgi-bin/file.py" method = "post">
  2. <p>File: <input type = "file" name = "filename" /></p>
  3. <p><input type = "submit" value = "Upload" /></p>
  4. </form>
  5.  
file.py

Expand|Select|Wrap|Line Numbers
  1. import cgi, os
  2. import cgitb; cgitb.enable()
  3. import pandas as pd
  4.  
  5. form = cgi.FieldStorage()
  6.  
  7. fileitem = form['filename']
  8. if fileitem.file:
  9.     message=fileitem.file.read()
  10.  
  11. #df = pd.read_csv(message, sep='\t')
  12. #some code for analysis
  13. #some code for analysis
  14. #some code for analysis
  15.  
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
Aug 21 '20 #1
Share this Question
Share on Google+
2 Replies

100+
P: 305
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.
Aug 23 '20 #2

P: 2
@madankarmukta Thank you for your response. fs message is working fine, its suppose open the file and read which its doing fine.
http://imgur.com/a/5ajd20b
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
Aug 24 '20 #3

Post your reply

Sign in to post your reply or Sign up for a free account.