Home > OS >  Python how to read xlsx file and convert into csv without writing to directory
Python how to read xlsx file and convert into csv without writing to directory

Time:12-18

I receive an xlsx file from an HTTP POST request and read it using pd.read_excel(). It's in unreadable binary format at first, so I convert it to csv using .to_csv()

import pandas as pd
import requests

response = http_post('https://data.bls.gov/pdq/SurveyOutputServlet')
xlsx = pd.read_excel(response.content)
xlsx.to_csv('outputname.csv', index=False)

This works, it gives me a readable version of the data, but the problem is to_csv() is also writing the file to my directory, and I don't want to save this data anywhere. I just want to get the file content using http, convert it to csv, work with that data in my script, then have it vanish once I no longer need it; no writing to other files.

Is there a way to do this? Do I even need to be converting to csv to get a readable representation of the xlsx data?

CodePudding user response:

CSV is a file format; there is no particular reason to want CSV in memory, except perhaps if you will eventually write it to disk but need to preprocess the bytes somehow before that. Simply reading the data into a Pandas data frame is almost certainly all you want or need here.

CodePudding user response:

import pandas as pd 
import requests 

response = http_post('https://data.bls.gov/pdq/SurveyOutputServlet')
xlsx = pd.read_excel(response.content) 
xlsx = xlsx.csv('outputname.csv', index=False)

CodePudding user response:

This should work: import pandas as pd

read_file = pd.read_excel (r'Path where the Excel file is stored\File name.xlsx')
read_file.to_csv (r'Path to store the CSV file\File name.csv', index = None, header=True)
  • Related