Python Forum
Read csv file using Pandas in Django
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read csv file using Pandas in Django
#1
Hello everyone,
Please I need advice from you. I trying read csv file using Pandas in Django. I need read whole file and also need read (find) on first line specific word. I tried read file twice, but I got error "No columns to parse from file".

This is my code:

def csv(request):
    if request.method == "POST" and 'myFile' in request.FILES:
        file = request.FILES["myFile"]
        csv = pd.read_csv(file, skiprows=0, sep=";")
        firstLine = pd.read_csv(file, skiprows=0, sep=";", nrows=0)

        dict = {
            'Unnamed: 0': "",
            'Unnamed: 1': "",
            'Unnamed: 2': "",
            'Unnamed: 3': "",
            'Unnamed: 4': "",
            'Unnamed: 5': "",
            'Unnamed: 6': "",
            'Unnamed: 7': "",
            'Unnamed: 8': "",
        }
        csv.rename(columns = dict, inplace = True)
        csv.fillna("", inplace=True)
        data = csv.to_html(index=False)

        return render(request, "index.html", {"dataLoaded": True, "data": data, "firstLine": firstLine })
    else:
        return render(request, "index.html", {"message": "Something is wrong!"})
Thank you for any help :)
Reply


Forum Jump:

User Panel Messages

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