Hướng dẫn python read xml file as text - python đọc tệp xml dưới dạng văn bản

Tôi có mã sau trong python [chỉ tải dữ liệu từ txt]:

def main[]:
    f = open["text.txt", "r" ] //load txt
    a = [] // new array
    for line in f:
        a.append[line.strip[]] //append line

main[]

Làm thế nào tôi có thể làm điều này với xml file? f = open["myxml.xml", "r" ] không hoạt động. Tôi nhận được lỗi: UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 4877: character maps to

Hỏi ngày 20 tháng 4 năm 2015 lúc 18:30Apr 20, 2015 at 18:30

3

Điều này không liên quan gì đến định dạng tệp XML, nhưng trong đó mã hóa tệp của bạn là. Python3 giả định mọi thứ ở UTF-8, nhưng nếu bạn đang ở trên Windows, tệp của bạn có thể nằm trong Windows-1252. Bạn nên sử dụng:

f = open["text.txt", "r", encoding="cp1252"]

Đã trả lời ngày 20 tháng 4 năm 2015 lúc 18:50Apr 20, 2015 at 18:50

Syntonsyntonymsyntonym

6.8662 Huy hiệu vàng27 Huy hiệu bạc44 Huy hiệu đồng2 gold badges27 silver badges44 bronze badges

3

Điều này chắc chắn sẽ làm công việc của bạn.

a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a

Đã trả lời ngày 20 tháng 4 năm 2015 lúc 19:00Apr 20, 2015 at 19:00

Ngôn ngữ đánh dấu có thể mở rộng, thường được gọi là XML là một ngôn ngữ được thiết kế đặc biệt để dễ dàng giải thích bởi cả con người và máy tính hoàn toàn. Ngôn ngữ xác định một tập hợp các quy tắc được sử dụng để mã hóa một tài liệu ở một định dạng cụ thể. Trong bài viết này, các phương thức đã được mô tả để đọc và viết các tệp XML bằng Python., commonly known as XML is a language designed specifically to be easy to interpret by both humans and computers altogether. The language defines a set of rules used to encode a document in a specific format. In this article, methods have been described to read and write XML files in python.

Lưu ý: Nói chung, quá trình đọc dữ liệu từ tệp XML và phân tích các thành phần logic của nó được gọi là phân tích cú pháp. Do đó, khi chúng tôi đề cập đến việc đọc một tệp XML, chúng tôi đang đề cập đến việc phân tích cú pháp tài liệu XML. & NBSP; In general, the process of reading the data from an XML file and analyzing its logical components is known as Parsing. Therefore, when we refer to reading a xml file we are referring to parsing the XML document

Trong bài viết này, chúng tôi sẽ xem xét hai thư viện có thể được sử dụng cho mục đích phân tích cú pháp XML. Họ đang:

  • Đẹp được sử dụng cùng với trình phân tích cú pháp LXML XML & NBSP;
  • Thư viện ElementTree. & NBSP;

Sử dụng đẹp cùng với trình phân tích cú pháp LXML

Với mục đích đọc và viết tệp XML, chúng tôi sẽ sử dụng thư viện Python có tên BeautifulSoup. Để cài đặt thư viện, hãy nhập lệnh sau vào thiết bị đầu cuối. & Nbsp; & nbsp;

pip install beautifulsoup4

Súp đẹp hỗ trợ trình phân tích cú pháp HTML có trong thư viện tiêu chuẩn Python, nhưng nó cũng hỗ trợ một số trình phân tích cú pháp Python của bên thứ ba. Một là trình phân tích cú pháp LXML [được sử dụng để phân tích các tài liệu XML/HTML]. LXML có thể được cài đặt bằng cách chạy lệnh sau trong bộ xử lý lệnh của hệ điều hành của bạn: & nbsp; & nbsp;

pip install lxml

Đầu tiên chúng ta sẽ học cách đọc từ tệp XML. Chúng tôi cũng sẽ phân tích dữ liệu được lưu trữ trong đó. Sau đó, chúng tôi sẽ học cách tạo một tệp XML và ghi dữ liệu vào nó. & NBSP;

Đọc dữ liệu từ tệp XML

Có hai bước cần thiết để phân tích tệp XML:-& nbsp; & nbsp;

  • Tìm thẻ & nbsp;
  • Trích xuất từ ​​thẻ

Example:

Tệp XML được sử dụng: & nbsp; 

Python3

f = open["text.txt", "r", encoding="cp1252"]
0
f = open["text.txt", "r", encoding="cp1252"]
1
f = open["text.txt", "r", encoding="cp1252"]
2
f = open["text.txt", "r", encoding="cp1252"]
3

f = open["text.txt", "r", encoding="cp1252"]
4
f = open["text.txt", "r", encoding="cp1252"]
5
f = open["text.txt", "r", encoding="cp1252"]
6
f = open["text.txt", "r", encoding="cp1252"]
7
f = open["text.txt", "r", encoding="cp1252"]
8
f = open["text.txt", "r", encoding="cp1252"]
9
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
0

a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
1
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
223
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
4

a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
5
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
7
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
8
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

pip install beautifulsoup4
0____23
pip install beautifulsoup4
2
pip install beautifulsoup4
3
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

pip install beautifulsoup4
5
pip install beautifulsoup4
6

pip install beautifulsoup4
7
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
pip install beautifulsoup4
9
pip install lxml
0
pip install lxml
1
pip install lxml
2
pip install lxml
3
pip install lxml
444

pip install beautifulsoup4
5
pip install lxml
7

pip install lxml
8
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3 python0python1
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

pip install beautifulsoup4
5python4

OUTPUT:

Viết tệp XML

Viết tệp XML là một quá trình nguyên thủy, lý do cho rằng thực tế là các tệp XML được mã hóa theo một cách đặc biệt. Sửa đổi các phần của tài liệu XML yêu cầu một người để phân tích thông qua nó lúc đầu. Trong mã dưới đây, chúng tôi sẽ sửa đổi một số phần của tài liệu XML đã nói ở trên. & NBSP;

Example:  

Python3

f = open["text.txt", "r", encoding="cp1252"]
0
f = open["text.txt", "r", encoding="cp1252"]
1
f = open["text.txt", "r", encoding="cp1252"]
2
f = open["text.txt", "r", encoding="cp1252"]
3

f = open["text.txt", "r", encoding="cp1252"]
4
f = open["text.txt", "r", encoding="cp1252"]
5
f = open["text.txt", "r", encoding="cp1252"]
6
f = open["text.txt", "r", encoding="cp1252"]
7
f = open["text.txt", "r", encoding="cp1252"]
8
f = open["text.txt", "r", encoding="cp1252"]
9
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
0

a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
1
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
223
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
4

a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
5
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
7
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
8
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

pip install beautifulsoup4
0____23
pip install beautifulsoup4
2
pip install beautifulsoup4
3
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

pip install beautifulsoup4
7
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
pip install beautifulsoup4
9
pip install lxml
0
pip install lxml
1
pip install lxml
2
pip install lxml
3
pip install lxml
444

pip install beautifulsoup4
5UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 4877: character maps to 2

Output:

pip install lxml
8
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3 python0python1
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

Viết tệp XML provides us with a plethora of tools for manipulating XML files. The best part about it being its inclusion in the standard Python’s built-in library. Therefore, one does not have to install any external modules for the purpose. Due to the xmlformat being an inherently hierarchical data format, it is a lot easier to represent it by a tree. The module provides ElementTree provides methods to represent whole XML document as a single tree. 

Viết tệp XML là một quá trình nguyên thủy, lý do cho rằng thực tế là các tệp XML được mã hóa theo một cách đặc biệt. Sửa đổi các phần của tài liệu XML yêu cầu một người để phân tích thông qua nó lúc đầu. Trong mã dưới đây, chúng tôi sẽ sửa đổi một số phần của tài liệu XML đã nói ở trên. & NBSP;

xml file?0
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
7xml file?3
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

Is

Example:    

Python3

f = open["text.txt", "r", encoding="cp1252"]
2 UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 4877: character maps to 4

UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 4877: character maps to 5

a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3 UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 4877: character maps to 7
f = open["text.txt", "r", encoding="cp1252"]
7
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

f = open["text.txt", "r", encoding="cp1252"]
00
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
f = open["text.txt", "r", encoding="cp1252"]
02

pip install beautifulsoup4
5
f = open["text.txt", "r", encoding="cp1252"]
04

pip install beautifulsoup4
5
f = open["text.txt", "r", encoding="cp1252"]
06
f = open["text.txt", "r", encoding="cp1252"]
07
f = open["text.txt", "r", encoding="cp1252"]
08

pip install beautifulsoup4
5
f = open["text.txt", "r", encoding="cp1252"]
06
f = open["text.txt", "r", encoding="cp1252"]
11
f = open["text.txt", "r", encoding="cp1252"]
12
f = open["text.txt", "r", encoding="cp1252"]
07
f = open["text.txt", "r", encoding="cp1252"]
14

Output:

Viết tệp XML

Bây giờ, chúng tôi sẽ xem xét một số phương pháp có thể được sử dụng để ghi dữ liệu trên tài liệu XML. Trong ví dụ này, chúng tôi sẽ tạo một tệp XML từ đầu. & NBSP;

Để làm tương tự, trước tiên, chúng tôi tạo một thẻ gốc [cha mẹ] dưới tên của cờ vua bằng lệnh et.element [‘cờ vua]. Tất cả các thẻ sẽ rơi xuống bên dưới thẻ này, tức là một khi thẻ gốc đã được xác định, các phần tử phụ khác có thể được tạo bên dưới nó. Sau đó, chúng tôi đã tạo một Subtag/SubLement có tên mở bên trong thẻ cờ bằng lệnh et.subelement []. Sau đó, chúng tôi đã tạo thêm hai khoản phụ khác ở bên dưới lỗ mở có tên E4 và D4. Sau đó, chúng tôi đã thêm các thuộc tính vào các thẻ E4 và D4 bằng SET [] là phương thức được tìm thấy bên trong SubEuity [], được sử dụng để xác định các thuộc tính cho một thẻ. Sau đó, chúng tôi đã thêm văn bản giữa các thẻ E4 và D4 bằng cách sử dụng văn bản thuộc tính được tìm thấy bên trong hàm SubEuity. Cuối cùng, chúng tôi đã chuyển đổi kiểu dữ liệu của các nội dung chúng tôi đang tạo từ 'xml.etree.elementtree.element' sang byte đối tượng, sử dụng lệnh et.tostring [] [mặc dù tên hàm là toString [] trong một số triển khai mà nó chuyển đổi Kiểu dữ liệu thành `byte` thay vì` str`]. Cuối cùng, chúng tôi đã xóa dữ liệu vào một tệp có tên gameofsquares.xml, đây là chế độ `wb` để cho phép viết dữ liệu nhị phân cho nó. Cuối cùng, chúng tôi đã lưu dữ liệu vào tệp của chúng tôi.

Example:    

Python3

f = open["text.txt", "r", encoding="cp1252"]
2 UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 4877: character maps to 4

a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
2
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
f = open["text.txt", "r", encoding="cp1252"]
19
f = open["text.txt", "r", encoding="cp1252"]
20
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

f = open["text.txt", "r", encoding="cp1252"]
22
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
f = open["text.txt", "r", encoding="cp1252"]
24
f = open["text.txt", "r", encoding="cp1252"]
25
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

f = open["text.txt", "r", encoding="cp1252"]
27
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
f = open["text.txt", "r", encoding="cp1252"]
29
f = open["text.txt", "r", encoding="cp1252"]
30
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

f = open["text.txt", "r", encoding="cp1252"]
32
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
f = open["text.txt", "r", encoding="cp1252"]
29
f = open["text.txt", "r", encoding="cp1252"]
35
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

f = open["text.txt", "r", encoding="cp1252"]
37
f = open["text.txt", "r", encoding="cp1252"]
38
f = open["text.txt", "r", encoding="cp1252"]
6
f = open["text.txt", "r", encoding="cp1252"]
40
f = open["text.txt", "r", encoding="cp1252"]
8
f = open["text.txt", "r", encoding="cp1252"]
42
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

f = open["text.txt", "r", encoding="cp1252"]
44
f = open["text.txt", "r", encoding="cp1252"]
38
f = open["text.txt", "r", encoding="cp1252"]
6
f = open["text.txt", "r", encoding="cp1252"]
40
f = open["text.txt", "r", encoding="cp1252"]
8
f = open["text.txt", "r", encoding="cp1252"]
49
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
9

f = open["text.txt", "r", encoding="cp1252"]
51
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
f = open["text.txt", "r", encoding="cp1252"]
53

f = open["text.txt", "r", encoding="cp1252"]
54
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
f = open["text.txt", "r", encoding="cp1252"]
56

f = open["text.txt", "r", encoding="cp1252"]
57
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
3
f = open["text.txt", "r", encoding="cp1252"]
59

f = open["text.txt", "r", encoding="cp1252"]
4
f = open["text.txt", "r", encoding="cp1252"]
5
f = open["text.txt", "r", encoding="cp1252"]
6
f = open["text.txt", "r", encoding="cp1252"]
63
f = open["text.txt", "r", encoding="cp1252"]
8
f = open["text.txt", "r", encoding="cp1252"]
65
a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
0

a=[]
with open['reboot.xml', 'r'] as f:
    a = f.read[]
f.closed
print a
1
f = open["text.txt", "r", encoding="cp1252"]
68

Output:


Bài Viết Liên Quan

Chủ Đề