简体   繁体   中英

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

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?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM