[PYTHON] Read the csv file and display it in the browser

Overview / Goals

I can't query the csv file in tabular format because I don't have Excel installed on my portable laptop. A spreadsheet can be used as a substitute, but you need an internet connection to display it in a spreadsheet, and you don't want to use unnecessary packet communication. Offline is enough for querying CSV files. When I googled variously, I found that python's pandas library can also read CSV files and output html. Create a program that reads a csv file and displays it on a browser for the purpose of studying python.

environment

Things to do, order to do

  1. Install pandas with pip
  2. Create a python program
  3. Display in browser

Install pandas with pip

Run the usual command pip install pandas in the VS Code terminal. For some reason an error occurs ... Because it is a character that draws an unexpected error before writing the source, it is expected that such an error will occur.

(1)
ERROR: Could not install packages due to an EnvironmentError: [WinError 5]Access denied.: 'c:\\program files\\python38\\lib\\site-packages\\pip-19.2.3.dist-info\\entry_points.txt'
Consider using the `--user` option or check the permissions.

(2)
WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

(1) ... Access is denied or permissions are displayed, so it's probably an authority. I gave up the terminal of VSCode, started the command prompt as an administrator and executed the pip command, and it was installed normally.

(2) A warning is issued because the version of pip installed with python3.8.1 is old. The command displayed in'' was executed, and the pip upgrade was performed before the execution of (1).

Create a python program

Read the CSV file with read_csv () of pandas, convert the CSV data to

with to_html (), and write it to html. If there is a space in the read CSV file, it will be treated as a missing value and converted to NaN, so do not convert it with na_filter = False. The CSV file name is displayed in the title of html.

main.py


import os
import pandas as pd
import sys

def main():
    #Get CSV file from argument 1
    args = sys.argv
    haserror = False
    csvfile = ''

    #No argument is an error
    if 1 == len(args):
        print("No CSV file is specified.")
        haserror = True
    else:
        csvfile = args[1]

        #Error if the file name is incorrect
        if os.path.exists(csvfile) == False:
            print("The specified file does not exist.")
            haserror = True
        
        #If it is not a CSV file, an error will occur.
        if csvfile.endswith(".csv") == False:
            print("A file other than the CSV file is specified.")
            haserror = True
        
    if haserror == True:
        return()
    else:
        print("Convert CSV file to html.")

    #Read CSV file
    csvdata = pd.read_csv(csvfile, na_filter=False)

    #Read html file
    htmldata = ''
    with open('templete.html',mode='r',encoding='utf-8') as htmlfile:
        htmldata = htmlfile.read()

    #Convert CSV file to html
    rpdict = { "filename" : os.path.basename(csvfile), "table" : csvdata.to_html() }
    htmldata = htmldata.format(**rpdict)

    #html file output
    with open('csvconverted.html',mode='w',encoding='utf-8') as outputhtml:
        outputhtml.write(htmldata)

if __name__== '__main__':
    main()

Before conversion html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title> {filename} </title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
        <div class="csvlist">
            {table}
        </div>
    </body>
</html>

After conversion html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title> test.csv </title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
        <div class="csvlist">
            <table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>date</th>
      <th>Holiday classification</th>
      <th>Attendance time</th>
      <th>Leave time</th>
      <th>working time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2019/12/1</td>
      <td>Law holiday</td>
      <td>09:00</td>
      <td>13:00</td>
      <td>04:00</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2019/12/2</td>
      <td></td>
      <td>09:00</td>
      <td>21:30</td>
      <td>11:30</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2019/12/3</td>
      <td></td>
      <td>09:00</td>
      <td>21:30</td>
      <td>11:30</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2019/12/4</td>
      <td></td>
      <td>09:00</td>
      <td>21:00</td>
      <td>11:00</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2019/12/5</td>
      <td></td>
      <td>09:00</td>
      <td>20:00</td>
      <td>10:00</td>
    </tr>
    <tr>
      <th>5</th>
      <td>2019/12/6</td>
      <td></td>
      <td>09:00</td>
      <td>20:00</td>
      <td>10:00</td>
    </tr>
    <tr>
      <th>6</th>
      <td>2019/12/7</td>
      <td>Public holiday</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <th>7</th>
      <td>2019/12/8</td>
      <td>Law holiday</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>
        </div>
    </body>
</html>

Looking back

Click here for the product https://github.com/otowmoyarng/FreeStudyRepo/tree/master/20191228-pythoncsvviewer

Recommended Posts

Read the csv file and display it in the browser
[Python] Read the csv file and display the figure with matplotlib
Django ~ Let's display it in the browser ~
Read and write csv file
The one that divides the csv file, reads it, and processes it in parallel
Read the VTK file and display the color map with jupyter.
Read the csv file with jupyter notebook and write the graph on top of it
Read all csv files in the folder
Read the file line by line in Python
Read the file line by line in Python
Find it in the procession and edit it
[pandas] .csv file reading and display method
[Python] Read the specified line in the file
Format the Git log and get the committed file name in csv format
Predict the amount of electricity used in 2 days and publish it in CSV
Read CSV file with Python and convert it to DataFrame as it is
In bash, "Delete the file if it exists".
Read CSV file: pandas
Read Python csv file
Various ways to read the last line of a csv file in Python
[Introduction to Pandas] Read a csv file without a column name and give it a column name
Fourier transform the wav file read by Python, reverse transform it, and write it again.
How to read a serial number file in a loop, process it, and graph it
Read the linked list in csv format with graph-tool
Read json file with Python, format it, and output json
Extract the lyrics information in the MP3 / MP4 file and save it in the lyrics file (* .lrc) for Sony walkman.
I want to replace the variables in the python template file and mass-produce it in another file.
Read and write a file
Write and read a file
Display Japanese in JSON file
Save the pystan model and results in a pickle file
Read the function name from the DB and execute it dynamically
Get the MIME type in Python and determine the file format
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Real-time display of server-side processing progress in the browser (implementation of progress bar)
Read zipline brand data from csv file and perform back test
Python in the browser: Brython's recommendation
Save the binary file in Python
Check if the configuration file is read in an easy-to-understand manner
Search the file name including the specified word and extension in the directory
Scraping the schedule of Hinatazaka46 and reflecting it in Google Calendar
Run Pylint and read the results
Read big endian binary in Python and convert it to ndarray
DeepL translate YouTube Live chat and display it in real time
Preserve and read order in PyYAML
Scraping the list of Go To EAT member stores in Fukuoka prefecture and converting it to CSV
Python CSV file reading and writing
Normalize the file that converted Excel to csv as it is.
The story of the "hole" in the file
Read a file in Python with a relative path from the program
Read the old Gakushin DC application Word file (.doc) from Python and try to operate it.
Read the config file in Go language! Introducing a simple sample
Scraping the list of Go To EAT member stores in Niigata prefecture and converting it to CSV
Ignore # line and read in pandas
Create and read messagepacks in Python
How to display bytes in the same way in Java and Python
[Cliff in 2025] The Ministry of Economy, Trade and Industry's "DX Report 2" was published, so I read it.
Replace the directory name and the file name in the directory together with a Linux command.
Read and format a csv file mixed with comma tabs with Python pandas
[Python] Sweet Is it sweet? About suites and expressions in the official documentation
Enclose the cat result in double quotes and put it in a variable