Sachin
Sachin

Reputation: 109

how to append new lines to html output file

I wrote a code which creates few lists(recording files) per userid and then write it to a html file

if get_excel == 'yes':
    download_file = {'user_id' : userid_c, 'Recording_name' : chaptername, 'Download_link' : all_downloadlinks, 'Recording_date_time' : created, 'Duration_in_minutes' : duration_array}
    df = pd.DataFrame(download_file)
    df["Download_link"] = df["Download_link"].apply(lambda x: "<a href='{0}'>click here</a>".format(x))
    df.to_html('Recordings.html', render_links=True,escape=False,)

 s += 1   

and then it loops back to other userid's in the list. when the second users lists are written to the html file, it overides the existing html file rather than appending the new values to the existing one.

How i can resolve this?

Upvotes: 1

Views: 511

Answers (1)

Barmar
Barmar

Reputation: 781741

Open the file once outside the loop, then write to that file with to_html().

with open('Recordings.html', 'w') as htmlfile:
    # other code ...
    if get_excel == 'yes':
        download_file = {'user_id' : userid_c, 'Recording_name' : chaptername, 'Download_link' : all_downloadlinks, 'Recording_date_time' : created, 'Duration_in_minutes' : duration_array}
        df = pd.DataFrame(download_file)
        df["Download_link"] = df["Download_link"].apply(lambda x: "<a href='{0}'>click here</a>".format(x))
        df.to_html(htmlfile, render_links=True,escape=False,)
    
    s += 1 
    # more code ...

Upvotes: 1

Related Questions