How to format the output with methods?

Question:

I have a combination of methods. And I try to fromat them correct.

So I have this functions:

from __future__ import print_function

import itertools
import locale
import operator
import re


i50 ="[' nna)nn nnFactuurnVerdi Import SchoolfruitnFactuur nr. : 71201nnrut ard wegetablesnx0c']"

fruit_words = ['Appels', 'Ananas', 'Peen Waspeen',
               'Tomaten Cherry', 'Sinaasappels',
               'Watermeloenen', 'Rettich', 'Peren', 'Peen', 'Mandarijnen', 'Meloenen', 'Grapefruit']

def total_amount_fruit_regex(format_=re.escape):
    return r"(d*(?:.d+)*)s*~?=?s*(" + '|'.join(
        format_(word) for word in fruit_words) + ')'

def total_fruit_per_sort():
    number_found = re.findall(total_amount_fruit_regex(), verdi50)

    fruit_dict = {}
    for n, f in number_found:
        fruit_dict[f] = fruit_dict.get(f, 0) + int(n)
        result = 'n'.join(f'{key}: {val}' for key, val in fruit_dict.items())   
    return result


def fruit_list(format_=re.escape):
        return "|".join(format_(word) for word in fruit_words)


def findallfruit(regex):
    return re.findall(regex, verdi50)


def verdi_total_number_fruit_regex():
    return rf"(d*(?:.d+)*)s*W+(?:{fruit_list()})"


def show_extracted_data_from_file():
    regexes = [     
       verdi_total_number_fruit_regex(),       
    ] 
    matches = [findallfruit(regex) for regex in regexes]    
    return "n".join(" t ".join(items) for items in zip(*matches)) + "t" + total_fruit_per_sort()
    

print(show_extracted_data_from_file())

this give as output:

16
360
6
75
9
688
22
80
160
320
6
75
9
688
22
80
160
320
160
61      Watermeloenen: 466
Appels: 688
Sinaasappels: 803

But I want them like this:

16       Watermeloenen: 466
360      Appels: 688
6        Sinaasappels: 803
75
9
688
22
80
160
320
6
75
9
688
22
80
160
320
160
61 

So that they are next to each other. But how to do this?

Asked By: mightycode Newton

||

Answers:

You’re concatenating total_fruit_per_sort() at the end of the join. You need to concatenate it to the first 3 items being joined.

You can split it into lines, then use itertools.zip_longest() to loop over this in parallel with the generator.

def show_extracted_data_from_file():
    regexes = [
       verdi_total_number_fruit_regex(),
    ]
    matches = [findallfruit(regex) for regex in regexes]
    fruit_list = total_fruit_per_sort().split("n")
    return "n".join(" t ".join(items) for items in zip_longest(*matches, fruit_list, fillvalue=''))
Answered By: Barmar
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.