Theo ý kiến của tôi, có 2 loại kỹ năng xử lý chuỗi tính toán rộng cần được giới thiệu. Đầu tiên trong số này là biểu thức chính quy, một cách tiếp cận dựa trên mẫu để khớp văn bản. Có rất nhiều phần giới thiệu tuyệt vời về biểu thức chính quy mà người ta có thể tìm kiếm, nhưng những người học trực quan có thể đánh giá cao tốc độ nhanh. ai Code-First Video giới thiệu về khóa học Xử lý ngôn ngữ tự nhiên về chủ đề này Show Kỹ năng xử lý chuỗi tính toán riêng biệt khác là có thể tận dụng thư viện chuẩn của ngôn ngữ lập trình nhất định để thao tác chuỗi cơ bản. Như vậy, bài viết này là một đoạn mồi xử lý chuỗi Python ngắn Lưu ý rằng phân tích văn bản có ý nghĩa vượt xa quá trình xử lý chuỗi và cốt lõi của các kỹ thuật nâng cao hơn này có thể không yêu cầu bạn phải tự thao tác văn bản thường xuyên. Tuy nhiên, xử lý dữ liệu văn bản là một phần quan trọng và tốn thời gian của dự án phân tích văn bản thành công và những kỹ năng xử lý chuỗi nêu trên sẽ là vô giá ở đây. Hiểu về xử lý tính toán văn bản ở mức cơ bản về mặt khái niệm là rất quan trọng để hiểu các kỹ thuật phân tích văn bản nâng cao hơn. Nhiều ví dụ sau đây sử dụng mô-đun chuỗi thư viện chuẩn của Python và do đó, việc sử dụng mô-đun này để tham khảo là một ý kiến hay Bảng tính tiện dụng này chứa tất cả mã trong bản PDF có thể tải xuống này Tước khoảng trắng s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))8 (trái), khoảng trắng ở cuối bằng s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))9 (phải) và cả ở đầu và cuối bằng Strip unwanted characters: This is a sentence with unwanted characters.0. s = ' This is a sentence with whitespace. \n' print('Strip leading whitespace: {}'.format(s.lstrip())) print('Strip trailing whitespace: {}'.format(s.rstrip())) print('Strip all whitespace: {}'.format(s.strip())) Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace. Quan tâm đến việc loại bỏ các ký tự khác với khoảng trắng? s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A'))) Strip unwanted characters: This is a sentence with unwanted characters. Đừng quên kiểm tra tài liệu về chuỗi Strip unwanted characters: This is a sentence with unwanted characters.1 nếu cần tách chuỗi Strip unwanted characters: This is a sentence with unwanted characters.2. s = 'KDnuggets is a fantastic resource' print(s.split()) s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))0 Theo mặc định, Strip unwanted characters: This is a sentence with unwanted characters.2 phân tách trên khoảng trắng, nhưng (các) chuỗi ký tự khác cũng có thể được chuyển vào s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))2 s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))3 Nối các phần tử danh sách thành một chuỗi s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))5 s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))6 Đó không phải là sự thật sao. Và nếu bạn muốn nối các phần tử danh sách với thứ gì đó không phải là khoảng trắng ở giữa? Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.0 Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.1 Đảo ngược một chuỗi Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.2 Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.3 Chuyển đổi chữ hoa và chữ thường Strip unwanted characters: This is a sentence with unwanted characters.5, Strip unwanted characters: This is a sentence with unwanted characters.6 và Strip unwanted characters: This is a sentence with unwanted characters.7. Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.4 Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.5 Kiểm tra tư cách thành viên chuỗi Strip unwanted characters: This is a sentence with unwanted characters.8. Cú pháp rất giống ngôn ngữ tự nhiên. Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.6 Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.7 Nếu bạn quan tâm hơn đến việc tìm vị trí của một chuỗi con trong một chuỗi (trái ngược với việc chỉ kiểm tra xem chuỗi con đó có được chứa hay không), thì phương thức chuỗi find() có thể hữu ích hơn Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.8 Strip leading whitespace: This is a sentence with whitespace. Strip trailing whitespace: This is a sentence with whitespace. Strip all whitespace: This is a sentence with whitespace.9 Theo mặc định, Strip unwanted characters: This is a sentence with unwanted characters.9 trả về chỉ mục của ký tự đầu tiên trong lần xuất hiện đầu tiên của chuỗi con và trả về s = 'KDnuggets is a fantastic resource' print(s.split())0 nếu không tìm thấy chuỗi con. Kiểm tra tài liệu để biết các chỉnh sửa có sẵn cho hành vi mặc định này Thay thế dây phụ s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))0 s = 'This is a sentence with unwanted characters.AAAAAAAA' print('Strip unwanted characters: {}'.format(s.rstrip('A')))1 Một đối số đếm tùy chọn có thể chỉ định số lần thay thế liên tiếp tối đa để thực hiện nếu cùng một chuỗi con xảy ra nhiều lần Các giá trị chuỗi được lưu trữ trong Python như thế nào?Chuỗi được lưu trữ dưới dạng các ký tự riêng lẻ trong một vị trí bộ nhớ liền kề . Nó có thể được truy cập từ cả hai hướng. tiến và lùi. Nhân vật không là gì ngoài biểu tượng. Chuỗi là kiểu dữ liệu bất biến trong Python, có nghĩa là một khi chuỗi được tạo thì không thể thay đổi được.
Làm thế nào để Python đọc một chuỗi?Python – Đọc tệp dưới dạng chuỗi . Mở tệp ở chế độ đọc. Gọi hàm open() sẵn có với đường dẫn tệp làm đối số. hàm open() trả về một đối tượng tệp Gọi phương thức read() trên đối tượng tệp. phương thức read() trả về toàn bộ nội dung của tệp dưới dạng một chuỗi Đóng tệp bằng cách gọi phương thức close() trên đối tượng tệp Các chuỗi được lưu trữ nội bộ trong Python như thế nào?Trả lời. Các chuỗi được lưu trữ nội bộ trong Python 3 như thế nào? . Điều đó có nghĩa là chúng là một chuỗi byte trong đó mỗi ký tự có thể là một, hai, ba hoặc bốn byte tùy thuộc vào trang Unicode mà các ký tự này đến từ đâu. as a Unicode sequence with a know codec. That means that they are a sequence of bytes where each character might be one, two, three or four bytes depending on which Unicode page this characters are from.
Có kiểu dữ liệu chuỗi trong Python không?Chuỗi là chuỗi dữ liệu ký tự. Kiểu chuỗi trong Python được gọi là str . |