Python Forum
Read CSV data into Pandas DataSet From Variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read CSV data into Pandas DataSet From Variable?
#8
Hi, I'm using Tornado web server and have a similar situation. There's not much to do actually.. the file's contents come as a bytestring. Assuming you've gotten the contents into a variable 'file1' as OP mentioned,

df = pd.read_csv( io.BytesIO(file1) )
should do the job. Do import io at the top of your code.

As to how I got that file's contents into a variable, here's a shortened snippet and I'm guessing the structure should be similar in your framework.

On the HTML side:
<p><input type="file" name="file1"></p>

Python side:
import pandas as pd
import io

# skipping the tornado specific code...

class hydGTFS(tornado.web.RequestHandler):
	def post(self):
		print( self.request.files['file1'][0]['filename'] )

		df = pd.read_csv( io.BytesIO( self.request.files['file1'][0]['body']) )
		print(df.head())
		self.write('ok got it bro')
Output:
Corridor 1 Week days detail..csv Run Id Run Description Trip Id Regulation Period Group Line Id 0 49 4901 6144 Default {new group} 47 1 49 4901 6144 Default {new group} 47 2 49 4901 6144 Default {new group} 47 3 49 4901 6144 Default {new group} 47 4 49 4901 6141 Default {new group} 37
Reply


Messages In This Thread
RE: Read CSV data into Pandas DataSet From Variable? - by answerquest - Jul-05-2018, 03:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping in pandas/multi-index data frame Aleqsie 3 689 Jan-06-2024, 03:55 PM
Last Post: deanhystad
Photo read matlab data pz16 1 1,468 Oct-06-2023, 11:00 PM
Last Post: snippsat
  Pandas read csv file in 'date/time' chunks MorganSamage 4 1,709 Feb-13-2023, 11:24 AM
Last Post: MorganSamage
Smile How to further boost the data read write speed using pandas tjk9501 1 1,270 Nov-14-2022, 01:46 PM
Last Post: jefsummers
Thumbs Up can't access data from URL in pandas/jupyter notebook aaanoushka 1 1,873 Feb-13-2022, 01:19 PM
Last Post: jefsummers
Question Sorting data with pandas TheZaind 4 2,357 Nov-22-2021, 07:33 PM
Last Post: aserian
  Pandas Data frame column condition check based on length of the value aditi06 1 2,700 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  [Pandas] Write data to Excel with dot decimals manonB 1 5,896 May-05-2021, 05:28 PM
Last Post: ibreeden
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 2,458 Feb-20-2021, 08:25 PM
Last Post: perfringo
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 2,231 Aug-03-2020, 02:05 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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