1

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 ="[' \n\na)\n\n \n\nFactuur\nVerdi Import Schoolfruit\nFactuur nr. : 71201\n\nrut ard wegetables\n\x0c']"

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?

mightycode Newton
  • 3,229
  • 3
  • 28
  • 54

1 Answers1

2

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=''))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Nice answer :) For printing it in a pretty way you can use many of the nice python tools available, look at this thread for example: https://stackoverflow.com/questions/9989334/create-nice-column-output-in-python I used `tabulate` to test your code, and it worked well. Note you'll need to keep the data in it's double-list stratuce and not as string and then print it, so: ``` res = zip_longest(*matches, fruit_list, fillvalue='') print(tabulate(res, headers=["first column name", "second column name"])) ``` – lmaayanl Nov 14 '22 at 22:48
  • @imaayanl. Ah, yes, Because I want to have it more tighter. Yes, indeed. Becasue the cost output is still not straight. Do you have an example with tabulate? for this specific case? Because yes, I also want to have headers above the data columns – mightycode Newton Nov 14 '22 at 22:55