Hướng dẫn how to create a table in html using python - cách tạo bảng trong html bằng python

Nếu bạn cần tạo bảng HTML từ dữ liệu tệp, thì đây là tập lệnh Python 3 cơ bản nên thực hiện thủ thuật. Nếu bạn sử dụng script shell, hãy xem bảng tạo bảng HTML bằng cách sử dụng hướng dẫn của Shell Script.

#!/usr/bin/python3

# Run as: create-html-table.py {input-file-name}
# The script requires 1 argument: the input file name.
# It expects a comma-separated input file to parse into an html table,
# and assumes that the column headers are located in the first row.

import sys

filein = open[sys.argv[1], "r"]
fileout = open["html-table.html", "w"]
data = filein.readlines[]

table = "\n"

# Create the table's column headers
header = data[0].split[","]
table += "  \n"
for column in header:
    table += "    {0}\n".format[column.strip[]]
table += "  \n"

# Create the table's row data
for line in data[1:]:
    row = line.split[","]
    table += "  \n"
    for column in row:
        table += "    \n".format[column.strip[]]
    table += "  \n"

table += "
{0}
" fileout.writelines[table] fileout.close[] filein.close[]

Tệp đầu vào

Tệp đầu vào phải chứa dữ liệu ở định dạng phân loại dấu phẩy với các tiêu đề cột nằm ở hàng đầu tiên. Ví dụ,

No.,Package,Priority,Is a Dependency?
1,adduser,important,yes
2,apt,important,yes
3,apt-utils,important,no
4,base-files,required,yes
5,base-passwd,required,yes
6,bash,required,no
7,bsdmainutils,important,no
8,bsdutils,required,yes
9,coreutils,required,yes
10,cpio,important,yes
11,cron,important,yes
12,dash,required,yes
13,debconf,required,yes
14,debconf-i18n,important,no
15,debian-archive-keyring,important,yes
16,debianutils,required,yes
17,diffutils,required,no
18,dmidecode,important,no
19,dmsetup,optional,yes
20,dpkg,required,yes

Tệp đầu ra

Tệp đầu ra sẽ được tạo trong thư mục Bash/PowerShell hiện tại và sẽ được đặt tên là HT HTML-Table.html. Tệp sẽ chứa dữ liệu tệp đầu vào được định dạng thành cấu trúc bảng HTML. Ví dụ,


    No.
    Package
    Priority
    Is a Dependency?
  
1 adduser important yes
2 apt important yes
3 apt-utils important no
4 base-files required yes
5 base-passwd required yes
6 bash required no
7 bsdmainutils important no
8 bsdutils required yes
9 coreutils required yes
10 cpio important yes
11 cron important yes
12 dash required yes
13 debconf required yes
14 debconf-i18n important no
15 debian-archive-keyring important yes
16 debianutils required yes
17 diffutils required no
18 dmidecode important no
19 dmsetup optional yes
20 dpkg required yes

Tệp đầu ra được hiển thị trong trang web CSS CSS

Dưới đây là bảng đầu ra Bảng HTML được hiển thị với định dạng CSS được sử dụng bởi trang web này.

No.Bưu kiệnQuyền ưu tiênLà một sự phụ thuộc?
1 thêm người dùngquan trọngVâng
2 đúng cáchquan trọngVâng
3 đúng cáchquan trọngVâng
4 đúng cáchapt-utilsVâng
5 đúng cáchapt-utilsVâng
6 đúng cáchapt-utilsVâng
7 đúng cáchquan trọngVâng
8 đúng cáchapt-utilsVâng
9 đúng cáchapt-utilsVâng
10 đúng cáchquan trọngVâng
11 đúng cáchquan trọngVâng
12 đúng cáchapt-utilsVâng
13 đúng cáchapt-utilsVâng
14 đúng cáchquan trọngVâng
15 đúng cáchquan trọngVâng
16 đúng cáchapt-utilsVâng
17 đúng cáchapt-utilsVâng
18 đúng cáchquan trọngVâng
19 đúng cáchapt-utilsVâng
20 đúng cáchapt-utilsVâng

đúng cách

Một bảng HTML được tạo với thẻ mở và thẻ đóng.Bên trong các thẻ này, dữ liệu được sắp xếp thành các hàng và cột bằng cách sử dụng các thẻ hàng mở và đóng bảng và mở và đóng các thẻ dữ liệu bảng.Thẻ hàng bảng được sử dụng để tạo một hàng dữ liệu.. Inside these tags, data is organized into rows and columns by using opening and closing table row tags and opening and closing table data tags. Table row tags are used to create a row of data.

Làm thế nào trích xuất bảng HTML từ Python?

Để trích xuất một bảng từ HTML, trước tiên bạn cần mở các công cụ nhà phát triển của mình để xem HTML trông như thế nào và xác minh xem nó có thực sự là một bảng chứ không phải một số yếu tố khác.Bạn mở các công cụ của nhà phát triển bằng khóa F12, xem tab Phần tử trực tuyến và làm nổi bật phần tử mà bạn quan tâm.open developer tools with the F12 key, see the “Elements” tab, and highlight the element you're interested in.

Bài Viết Liên Quan

Chủ Đề