Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unpack operation not allowed in this context Pylance

I have a function like this:

def post(self, request):
    submitted_form = ProfileForm(request.POST, request.FILES)
    content = ''
    apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
    ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
    peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'

    if submitted_form.is_valid():
        uploadfile = UploadFile(image=request.FILES["upload_file"])

        name_of_file = str(request.FILES['upload_file'])
        uploadfile.save()
        print('path of the file is:::', uploadfile.image.name)          

        with open(os.path.join(settings.MEDIA_ROOT,
                               f"{uploadfile.image}"), 'r') as f:

            print("Now its type is ", type(name_of_file))
            print(uploadfile.image.path)

            # reading PDF file
            if name_of_file.endswith('.pdf'):
                pdfFile = wi(filename=uploadfile.image.path,
                             resolution=300)
                text_factuur_verdi = []

                image = pdfFile.convert('jpeg')
                imageBlobs = []

                for img in image.sequence:
                    imgPage = wi(image=img)
                    imageBlobs.append(imgPage.make_blob('jpeg'))

                for imgBlob in imageBlobs:
                    image = Image.open(io.BytesIO(imgBlob))
                    text = pytesseract.image_to_string(image, lang='eng')
                    text_factuur_verdi.append(text)

                    totalString = re.findall(ExtractingTextFromFile.make_pattern(
                        apples_royal_gala), (*text_factuur_verdi[0])) + re.findall(ExtractingTextFromFile.make_pattern(
                            ananas_crownless), (*text_factuur_verdi[0]))

                    content = totalString
            # ENDING Reading pdf file

            else:
                content = f.read()
                print(content)

        return render(request, "main/create_profile.html", {
            'form': ProfileForm(),
            "content": content
        })

    return render(request, "main/create_profile.html", {
        "form": submitted_form,
    })

and the output is this:

['3.304,08', '3.962,00']

But I want to have the values without brackets(‘[]’).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So I try to unpack the values from the list with the ‘*’.

But then I get this error:

Unpack operation not allowed in this context  Pylance

So my question is: how to tackle this?

>Solution :

join is what you’re looking for:

...
output = ['3.304,08', '3.962,00']
unpacked = ", ".join(output)
print(unpacked)
> '3.304,08, 3.962,00'

If you want "'3.304,08', '3.962,00'" as the output, you will need to do

unpacked = ", ".join(["'"+e+"'" for e in output])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading