Unpack operation not allowed in this context Pylance

Question:

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(‘[]’).

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?

Asked By: mightycode Newton

||

Answers:

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])
Answered By: Oğuzhan Akan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.