AnsweredAssumed Answered

Need help with a loop

Question asked by vonmoosa on Oct 23, 2018
Latest reply on Oct 23, 2018 by vonmoosa

I have a script that reads parts of file names in a folder and creates an .xlsx file with that data in a table. The table gets created with only one file entry even though I'm testing with 2 files. Any ideas on how to tweak this code to get the desired output?


import os, sys, pandas as pd

path = r"C:\Users\anthonyv\Downloads\Metro\New folder"

files = os.listdir(path)

for filename in files:
    f = filename.split(' MP ')
    address = f[0]
    mile = '.'.join(f[1].split(' ')[0].split('_'))

    df = pd.DataFrame({'MP': ["{}".format(mile)],'Road': ["{}".format(address)]})

writer = pd.ExcelWriter('MetroReportAGOL.xlsx')
df.to_excel(writer, sheet_name='Sheet1')

print ("Complete")