Hướng dẫn read and write html file in python - đọc và ghi tệp html trong python

Cải thiện bài viết

Lưu bài viết

Ngôn ngữ Python có những ứng dụng tuyệt vời ngày nay trong hầu hết mọi lĩnh vực, nó có thể được sử dụng cùng với các công nghệ khác để làm cho cuộc sống của chúng ta dễ dàng hơn. Một cách sử dụng Python như vậy là nhận được đầu ra dữ liệu trong tệp HTML. language has great uses today in almost every field, it can be used along with other technologies to make our lives easier. One such use of python is getting the data output in an HTML file.

Chúng tôi có thể lưu bất kỳ số lượng dữ liệu đầu vào của chúng tôi vào tệp HTML trong Python bằng các ví dụ sau theo hai cách.

Ví dụ 1: Tạo tệp HTML và lưu dữ liệu đầu vào vào đó.

Approach:

  • Tạo một tệp HTML.
Function_Name = open["Complete_File_Name","File_operation"]
  • Thêm dữ liệu đầu vào ở định dạng HTML vào tệp.
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
  • Lưu tệp HTML.
Function_Name.close[]
  • Mở tệp HTML từ vị trí đã lưu.

Dưới đây là việc thực hiện:

Python3

Func =

Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
4
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
5
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
6
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
7
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
8

Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
9
Function_Name.close[]
0
Function_Name.close[]
1
Function_Name.close[]
2
Function_Name.close[]
1
Function_Name.close[]
4
Function_Name.close[]
1
Function_Name.close[]
6
Function_Name.close[]
1
Function_Name.close[]
8

Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
9
Function_Name = open["Complete_File_Name","File_operation"]
Function_Name.close[]
0
Function_Name.close[]
1
Function_Name = open["Complete_File_Name","File_operation"]
Function_Name.close[]
2___

Function_Name = open[File_Location,"File_operation"]
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
1

Output:

Tệp GFG-1.html được tạo trong thư mục file is created in the folder

Kiểm tra dữ liệu đầu ra của tệp GFG-1.htmlGFG-1.html file

Ví dụ 2: Tạo và lưu tệp HTML và sau đó thêm dữ liệu đầu vào vào đó.

Approach:

  • Tạo một tệp HTML và lưu nó.
Function_Name = open["Complete_File_Name","File_operation"]
Function_Name.close[]
  • Mở tệp HTML từ vị trí đã lưu.
  • Dưới đây là việc thực hiện:
Function_Name = open[File_Location,"File_operation"]
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
  • Lưu tệp HTML.
Function_Name.close[]
  • Mở tệp HTML từ vị trí đã lưu.

Dưới đây là việc thực hiện:

Python3

Func =

Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
4
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
5
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
6
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
7
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
8

Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
9
Function_Name.close[]
0
Function_Name.close[]
1
Function_Name.close[]
2
Function_Name.close[]
1Func 0

Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
9Func 2=

Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
9
Function_Name = open["Complete_File_Name","File_operation"]
Function_Name.close[]
0
Function_Name.close[]
1
Function_Name = open["Complete_File_Name","File_operation"]
Function_Name.close[]
2___

Function_Name = open[File_Location,"File_operation"]
Function_Name.write["Adding_Input_data_using_HTML_Synatx_separted_by_/n"]
1

Output:

Tệp GFG-1.html được tạo trong thư mụcGFG-2.html file

Kiểm tra dữ liệu đầu ra của tệp GFG-1.htmlGFG-2.html file

Ví dụ 2: Tạo và lưu tệp HTML và sau đó thêm dữ liệu đầu vào vào đó.output data into an HTML file using a simple function in python. Any one of the above methods can be used to get the desired result.


Bạn có thể đọc một tệp HTML trong Python không?

Nếu bạn muốn đọc tệp HTML dưới dạng chuỗi, bạn cần chuyển đổi kết quả bằng phương thức Decode [] của Python: Nhập Urllib.yêu cầu như r.Trang = r.you need to convert the result using Python's decode[] method: import urllib. request as r. page = r.

Chúng ta có thể viết mã HTML bằng Python không?

Nếu bạn quan tâm đến việc phát triển web với Python, thì việc biết HTML và CSS sẽ giúp bạn hiểu các khung web như Django và Flask tốt hơn.Nhưng ngay cả khi bạn mới bắt đầu với Python, HTML và CSS cũng có thể cho phép bạn tạo các trang web nhỏ để gây ấn tượng với bạn bè.HTML and CSS can enable you to create small websites to impress your friends.

Làm thế nào truyền dữ liệu từ tệp HTML sang tệp Python?

Bạn có thể truyền thông tin bằng cách kết hợp các cặp khóa và giá trị cùng với bất kỳ URL nào hoặc bạn có thể sử dụng thẻ HTML để truyền thông tin bằng phương thức GET.concatenating key and value pairs along with any URL or you can use HTML tags to pass information using GET method.

Bài Viết Liên Quan

Chủ Đề