Home > Software engineering >  Python loop generating Excelfiles
Python loop generating Excelfiles

Time:06-30

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?

Thank you in advance.

Regards

CodePudding user response:

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')
  • Related