Home > database >  read in csv files from a folder and create html files
read in csv files from a folder and create html files

Time:02-26

I'm new to python and hoping for some help to read in csv files from a folder and converting each file to a html folder...this is what I have so far:

import pandas as pd import os import glob

path = "htmlplots" csv_files = glob.glob(os.path.join(path, "*.csv"))

for file in csv_files: # read the csv file df = pd.read_csv(file) # print the filename print('File Name:', file.split("\")[-1]) # print the content display(df)

Ideally I then need to create html files from the resulting csv files that have a 'next' and 'previous' link from one to two, two to three (next) and three to two, two to one (previous).

CodePudding user response:

Use:

import pandas as pd
import os
import glob

path = ""

csv_files = glob.glob(os.path.join(path, "*.csv"))
for i, file in enumerate(csv_files):
    df = pd.read_csv(file, header = None)
    name = file.split('.')[-1]
    
    
    if i>0:
        prev = csv_files[i-1]
        df.loc['prev',:]=f'http://{prev}'
    else:
        df.loc['prev',:]=''
    if i!=len(csv_files)-1:
        next = csv_files[i 1]
        df.loc['next',:]=f'http://{next}'
    else:
        df.loc['next',:]=''
    df.to_html(f"{file}.html", render_links = True)

Input csv file:

enter image description here

Output html:

enter image description here

  • Related