[英]How to read a .sav file sent to the Django POST request
我正在使用 Django 讀取在 POST 請求中傳遞的文件。 但是,當我執行pyreadstat.read_sav(file) 時,我得到TypeError: Argument 'filename_path' has wrong type (expected str, got InMemoryUploadedFile)
我可以先存儲在 Django 的 default_storage 中並讀取為 pyreadstat.read_sav(file_url_in_default_storage) 但是有沒有辦法在不存儲在默認存儲中的情況下做到這一點?
我找不到更優雅的解決方案,而是通過創建一個臨時文件來解決,如https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp 所述
temp_file, temp_file_path = tempfile.mkstemp()
for chunk in file.chunks():
os.write(temp_file, chunk)
df, sav_file_meta_data = pyreadstat.read_sav(temp_file_path)
os.remove(temp_file_path) # removing temporary file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.