Reputation: 3949
I try to show the names of fruit with the cost of the fruit in a list.
So I have it like this:
import re
50="[' all apply. The Rotterdam District Court shall have exclusive jurisdiction.\n\nrut ard wegetables\n\x0c']"
fruit_words = ['Appels', 'Ananas', 'Peen Waspeen',
'Tomaten Cherry', 'Sinaasappels',
'Watermeloenen', 'Rettich', 'Peren', 'Peen', 'Mandarijnen', 'Meloenen', 'Grapefruit']
def fruit_list(format_=re.escape):
return "|".join(format_(word) for word in fruit_words)
def verdi_total_fruit_cost_regex():
fruit_list2 = fruit_list(format_="(?:{})".format)
return regex_fruit_cost(f"(?:{fruit_list2})")
def findallfruit(regex):
return re.findall(regex, verdi50)
def regex_fruit_cost(subst):
return rf"(?:{subst}).*?(?P<number>[0-9,.]*)\n" #\W+({fruit_list()})\n"
def show_extracted_data_from_file():
regexes = [
verdi_total_fruit_cost_regex()
]
matches = [findallfruit(regex) for regex in regexes]
return "\n".join(" \t ".join(items) for items in zip(*matches))
print(show_extracted_data_from_file())
And this is the output:
123,20
2.772,00
46,20
577,50
69,30
3.488,16
137,50
500,00
1.000,00
2.000,00
1.000,00
381,25
But how to show the cost with the name of the fruit sort in a list? So I mean like this:
[[123,20, Watermeloen], [2772,00, Watermeloen]], etc..
Upvotes: 1
Views: 69
Reputation: 2471
The issue is with the verdi_total_fruit_cost_regex()
function, you have added the fruit name to the non-capturing group(?:
), so it won't be captured as part of the regex capture group.
Even though you are doing OR
(|
) across fruit names, you have a single regex pattern and not multiple ones.
Updated the two marked lines -
import re
verdi50="[' \n\na)\n\n \n\nFactuur\nVerdi Import Schoolfruit\nFactuur nr. : 71201 Koopliedenweg 33\nDeb. nr. : 108636 2991 LN BARENDRECHT\nYour VAT nr. : NL851703884B01 Nederland\nFactuur datum : 10-12-21\nAantal Omschrijving Prijs Bedrag\nOrder number : 77553 Loading date : 09-12-21 Incoterm: : FOT\nYour ref. : SCHOOLFRUIT Delivery date :\nWK50\nD.C. Schoolfruit\n16 Watermeloenen Quetzali 16kg 4 IMPERIAL BR I € 7,70 € 123,20\n360 Watermeloenen Quetzali 16kg 4 IMPERIAL BR I € 7,70 € 2.772,00\n6 Watermeloenen Quetzali 16kg 4 IMPERIAL BR I € 7,/0 € 46,20\n75 Watermeloenen Quetzali 16kg 4 IMPERIAL BR I € 7,70 € 577,50\n9 Watermeloenen Quetzali 16kg 4 IMPERIAL BR I € 7,70 € 69,30\n688 Appels Royal Gala 13kg 60/65 Generica PL I € 5,07 € 3.488,16\n22 Sinaasappels Valencias 15kg 105 Elara ZAI € 6,25 € 137,50\n80 Sinaasappels Valencias 15kg 105 Elara ZAI € 6,25 € 500,00\n160 Sinaasappels Valencias 15kg 105 FVC ZAI € 6,25 € 1.000,00\n320 Sinaasappels Valencias 15kg 105 Generica ZAI € 6,25 € 2.000,00\n160 Sinaasappels Valencias 15kg 105 Noordhoek ZA I € 6,25 € 1.000,00\n61 Sinaasappels Valencias 15kg 105 Noordhoek ZA I € 6,25 € 381,25\nTotaal Colli Totaal Netto Btw Btw Bedrag Totaal Bedrag\n€ 12.095,11 1.088,56\nBetaling binnen 30 dagen\nAchterstand wordt gemeld bij de kredietverzekeringsmaatschappij\nVerDi Import BV ING Bank NV. Rotterdam IBAN number: NL17INGB0006959173 ~~\n\n \n\nKoopliedenweg 38, 2991 LN Barendrecht, The Netherlands SWIFT/BIC: INGBNL2A, VAT number: NL851703884B01 i\nTel, +31 (0}1 80 61 88 11, Fax +31 (0)1 8061 88 25 Chamber of Commerce Rotterdam no. 55424309 VerDi\n\nE-mail: [email protected], www.verdiimport.nl Dutch law shall apply. The Rotterdam District Court shall have exclusive jurisdiction.\n\nrut ard wegetables\n\x0c']"
fruit_words = ['Appels', 'Ananas', 'Peen Waspeen',
'Tomaten Cherry', 'Sinaasappels',
'Watermeloenen', 'Rettich', 'Peren', 'Peen', 'Mandarijnen', 'Meloenen', 'Grapefruit']
def fruit_list(format_=re.escape):
return "|".join(format_(word) for word in fruit_words)
def verdi_total_fruit_cost_regex():
fruit_list2 = fruit_list(format_="(?:{})".format)
return regex_fruit_cost(f"({fruit_list2})") # updated to remove the :?
def findallfruit(regex):
return re.findall(regex, verdi50)
def regex_fruit_cost(subst):
return rf"(?:{subst}).*?(?P<number>[0-9,.]*)\n" #\W+({fruit_list()})\n"
def show_extracted_data_from_file():
regexes = [
verdi_total_fruit_cost_regex()
]
matches = [findallfruit(regex) for regex in regexes]
return matches[0] # updated to get the required format
print(show_extracted_data_from_file())
Output:
[('Watermeloenen', '123,20'), ('Watermeloenen', '2.772,00'), ('Watermeloenen', '46,20'), ('Watermeloenen', '577,50'), ('Watermeloenen', '69,30'), ('Appels', '3.488,16'), ('Sinaasappels', '137,50'), ('Sinaasappels', '500,00'), ('Sinaasappels', '1.000,00'), ('Sinaasappels', '2.000,00'), ('Sinaasappels', '1.000,00'), ('Sinaasappels', '381,25')]
Upvotes: 1