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"]
1f = open["text.txt", "r", encoding="cp1252"]
2 f = open["text.txt", "r", encoding="cp1252"]
3f = open["text.txt", "r", encoding="cp1252"]
4f = open["text.txt", "r", encoding="cp1252"]
5f = open["text.txt", "r", encoding="cp1252"]
6f = open["text.txt", "r", encoding="cp1252"]
7f = open["text.txt", "r", encoding="cp1252"]
8f = open["text.txt", "r", encoding="cp1252"]
9a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
0a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
1a=[]
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
4a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
5a=[]
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
7a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
8a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9pip install beautifulsoup40____23
pip install beautifulsoup42
pip install beautifulsoup43
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9pip install beautifulsoup45
pip install beautifulsoup46
pip install beautifulsoup47
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 pip install beautifulsoup49
pip install lxml0
pip install lxml1
pip install lxml2
pip install lxml3
pip install lxml444
pip install beautifulsoup45
pip install lxml7
pip install lxml8
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 python
0python
1a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9pip install beautifulsoup45
python
4OUTPUT:
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"]
1f = open["text.txt", "r", encoding="cp1252"]
2 f = open["text.txt", "r", encoding="cp1252"]
3f = open["text.txt", "r", encoding="cp1252"]
4f = open["text.txt", "r", encoding="cp1252"]
5f = open["text.txt", "r", encoding="cp1252"]
6f = open["text.txt", "r", encoding="cp1252"]
7f = open["text.txt", "r", encoding="cp1252"]
8f = open["text.txt", "r", encoding="cp1252"]
9a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
0a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
1a=[]
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
4a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
5a=[]
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
7a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
8a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9pip install beautifulsoup40____23
pip install beautifulsoup42
pip install beautifulsoup43
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9pip install beautifulsoup47
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 pip install beautifulsoup49
pip install lxml0
pip install lxml1
pip install lxml2
pip install lxml3
pip install lxml444
pip install beautifulsoup45
UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 4877: character maps to
2Output:
pip install lxml
8a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 python
0python
1a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
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?
0a=[]
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?
3a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
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
4UnicodeDecodeError: '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
7f = open["text.txt", "r", encoding="cp1252"]
7a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9f = open["text.txt", "r", encoding="cp1252"]
00a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 f = open["text.txt", "r", encoding="cp1252"]
02pip install beautifulsoup45
f = open["text.txt", "r", encoding="cp1252"]
04pip install beautifulsoup45
f = open["text.txt", "r", encoding="cp1252"]
06f = open["text.txt", "r", encoding="cp1252"]
07f = open["text.txt", "r", encoding="cp1252"]
08pip install beautifulsoup45
f = open["text.txt", "r", encoding="cp1252"]
06f = open["text.txt", "r", encoding="cp1252"]
11f = open["text.txt", "r", encoding="cp1252"]
12f = open["text.txt", "r", encoding="cp1252"]
07f = 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
4a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
2a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 f = open["text.txt", "r", encoding="cp1252"]
19f = open["text.txt", "r", encoding="cp1252"]
20a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9f = open["text.txt", "r", encoding="cp1252"]
22a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 f = open["text.txt", "r", encoding="cp1252"]
24f = open["text.txt", "r", encoding="cp1252"]
25a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9f = open["text.txt", "r", encoding="cp1252"]
27a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 f = open["text.txt", "r", encoding="cp1252"]
29f = open["text.txt", "r", encoding="cp1252"]
30a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9f = open["text.txt", "r", encoding="cp1252"]
32a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 f = open["text.txt", "r", encoding="cp1252"]
29f = open["text.txt", "r", encoding="cp1252"]
35a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9f = open["text.txt", "r", encoding="cp1252"]
37f = open["text.txt", "r", encoding="cp1252"]
38f = open["text.txt", "r", encoding="cp1252"]
6f = open["text.txt", "r", encoding="cp1252"]
40f = open["text.txt", "r", encoding="cp1252"]
8f = open["text.txt", "r", encoding="cp1252"]
42a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9f = open["text.txt", "r", encoding="cp1252"]
44f = open["text.txt", "r", encoding="cp1252"]
38f = open["text.txt", "r", encoding="cp1252"]
6f = open["text.txt", "r", encoding="cp1252"]
40f = open["text.txt", "r", encoding="cp1252"]
8f = open["text.txt", "r", encoding="cp1252"]
49a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
9f = open["text.txt", "r", encoding="cp1252"]
51a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 f = open["text.txt", "r", encoding="cp1252"]
53f = open["text.txt", "r", encoding="cp1252"]
54a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 f = open["text.txt", "r", encoding="cp1252"]
56f = open["text.txt", "r", encoding="cp1252"]
57a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
3 f = open["text.txt", "r", encoding="cp1252"]
59f = open["text.txt", "r", encoding="cp1252"]
4f = open["text.txt", "r", encoding="cp1252"]
5f = open["text.txt", "r", encoding="cp1252"]
6f = open["text.txt", "r", encoding="cp1252"]
63f = open["text.txt", "r", encoding="cp1252"]
8f = open["text.txt", "r", encoding="cp1252"]
65a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
0a=[]
with open['reboot.xml', 'r'] as f:
a = f.read[]
f.closed
print a
1f = open["text.txt", "r", encoding="cp1252"]
68Output: