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

Python loop generating Excelfiles

i have following code:

#import requests
from pandas import pandas as pd
base_url = "https://baden.liga.nu/cgi-bin/WebObjects/nuLigaTENDE.woa/wa/groupPage?championship=B2+S+2022&group="
Gruppe = ["1","24"] 
Team = ["H1","H2"] 

#Tabelle & Spielplan
for element in Gruppe:
    df = pd.read_html(base_url+str(element), index_col=0)
    for name in Team:
        df[0].to_csv('Exporte/Gruppe_'+str(element)+'_'+str(name)+'_Tabelle.csv', index = False)
        df[1].to_csv('Exporte/Gruppe_'+str(element)+'_'+str(name)+'_Spielplan.csv', index = False)

I only need the excel files generated for Gruppe "1"/ Team "H1" as well as Gruppe "2"/ Team "H2" but not for Gruppe "1"/ Team "H2" and not for Gruppe "2"/ Team "H1"

Anyone able to tell me how to setup the loop to get only the desired Excelfiles?

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

Thank you in advance.

Regards

>Solution :

You could use zip():

#import requests
from pandas import pandas as pd
base_url = "https://baden.liga.nu/cgi-bin/WebObjects/nuLigaTENDE.woa/wa/groupPage?championship=B2+S+2022&group="
Gruppe = ["1","24"] 
Team = ["H1","H2"] 

#Tabelle & Spielplan
for element,name in zip(Gruppe, Team):
    df = pd.read_html(base_url+str(element), index_col=0)
    df[0].to_csv('Exporte/Gruppe_'+str(element)+'_'+str(name)+'_Tabelle.csv', index = False)
    df[1].to_csv('Exporte/Gruppe_'+str(element)+'_'+str(name)+'_Spielplan.csv', index = False)

From the Python doc: zip iterates over several iterables in parallel, producing tuples with an item from each one.

An example from the doc:

for item in zip([1, 2, 3], ['sugar', 'spice', 'everything nice']):
    print(item)

>>> (1, 'sugar')
>>> (2, 'spice')
>>> (3, 'everything nice')
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