1

I'm reading a .xlsx file and converting it to .csv but I need to return it as a .csv because I don't have access to the path where it should be stored. I've only been able to return it as a dataframe. Any help will be really appreciated. Thanks.

import pandas as pd


def excel_to_csv(myfile):
    print(myfile)
    data_xls = pd.read_excel(myfile,  index_col=None)
    data_xls.to_csv(encoding='utf-8', index=False)
    return(data_xls)

myfile = request.FILES['file']
if myfile.name.endswith('.xlsx'):
    myfile=excel_to_csv(myfile)



89f3a1c
  • 1,430
  • 1
  • 14
  • 24
  • See this: converting into a byteio object. https://stackoverflow.com/questions/52089872/how-to-convert-pandas-dataframe-to-bytes-like-object. But, what I don't understand is, if you don't have a path, how will you save the csv finally? It's a file and needs to go to somewhere on some file system! You would need the path at some point. Don't you? – CypherX Oct 14 '19 at 21:31
  • I do have a path but I don't know where is going to be storage because is in a docker container that I don't have access to. Thanks – Masashimaru Oct 14 '19 at 21:42
  • ' I need to return it as a .csv because I don't have access to the path where it should be stored.' The fact that you don't have access to the path where it should be stored shouldn't have anything to do with the output format. It's not really clear to me what you're dealing with and what you want to achieve. – 89f3a1c Oct 14 '19 at 22:01

2 Answers2

1

if you just want to read an Excel file and get the same data as csv printed to your screen, the following might be useful.

def excel_to_csv(file):
    df = pd.read_excel(file)
    csv_data = df.to_csv(encoding='utf-8', index=False)
    return csv_data

If you really want to save the file I think I would need more information to the systems your are using and what exactly are you trying to accomplish.

daco
  • 600
  • 1
  • 4
  • 14
1

If you're using Django, just save it into /media/ root, then you can access it via url

First you need to know the config of MEDIA_ROOT & MEDIA_URL inside your settings.py.

If it's not exist, then you can create one

[ settings.py ]

MEDIA_ROOT = os.path.join(BASE_DIR, 'myproject/static')
MEDIA_URL = '/media/'

Then, make sure you're already add the media config into your urls.py

from django.conf.urls.static import static

urlpatterns = [
    # Project url patterns...
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Since you're mentioned this code myfile = request.FILES['file'], then I assume you're in views.py. Last but not least we need FileSystemStorage API.

There you go

[ views.py ]

from django.core.files.storage import FileSystemStorage

def upload(request):
    if request.method == "POST":
        csv_file = request.FILE['file']
        fs = FileSystemStorage()
        name = fs.save(csv_file.name, csv_file)
        print(f"file_name = {fs.url(name)}"
    return render(request, 'somepage.html')

Access the file via url, www.yourhost.com/media/file_name