Hướng dẫn how do i extract text from a character in python? - làm cách nào để trích xuất văn bản từ một ký tự trong python?

Một giải pháp regex cho vui:

>>> import re
>>> re.findall[r'@[\w+]', '@Hello there @bob @!']
['Hello', 'bob']
>>> re.findall[r'@[\w+]', 'Hello there bob !']
[]
>>> [re.findall[r'@[\w+]', 'Hello there @bob !'] or None,][0]
'bob'
>>> print [re.findall[r'@[\w+]', 'Hello there bob !'] or None,][0]
None

Regex ở trên sẽ nhận các mẫu của một hoặc nhiều ký tự chữ và số theo ký tự '@' cho đến khi tìm thấy một ký tự không phải là vô sinh.

Dưới đây là giải pháp Regex để khớp với một hoặc nhiều ký tự không phải là màu nếu bạn muốn chụp một phạm vi phụ hơn:

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']

Lưu ý rằng khi Regex ở trên gặp một chuỗi như @xyz@abc, nó sẽ nắm bắt xyz@abc trong một kết quả thay vì xyzabc riêng biệt. Để khắc phục điều đó, bạn có thể sử dụng lớp ký tự \s phủ định đồng thời phủ định các ký tự @:

>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']

Và đây là một giải pháp Regex để khớp với một hoặc nhiều ký tự bảng chữ cái chỉ trong trường hợp bạn không muốn có bất kỳ số nào hoặc bất cứ thứ gì khác:

>>> re.findall[r'@[[A-Za-z]+]', '@Hello there @bobv2.0 @!']
['Hello', 'bobv']

Đôi khi chúng ta đi qua các tình huống mà chúng ta yêu cầu để có được tất cả các từ có trong chuỗi, đây có thể là một nhiệm vụ tẻ nhạt được thực hiện bằng phương pháp gốc. Do đó có tốc ký để thực hiện nhiệm vụ này luôn hữu ích. Ngoài ra, bài viết này cũng bao gồm các trường hợp trong đó các dấu chấm câu phải bị bỏ qua. Phương pháp nếu người ta muốn hoàn thành nhiệm vụ cụ thể này. Nhưng nhược điểm là nó thất bại trong các trường hợp chuỗi chứa dấu chấm câu. & NBSP;
Method #1 : Using split[] 
Using the split function, we can split the string into a list of words and this is the most generic and recommended method if one wished to accomplish this particular task. But the drawback is that it fails in cases the string contains punctuation marks.
 

Python3

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
0____11
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
2

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
5 ________ 16 & nbsp;
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
7

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
8
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
1 xyz2xyz3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz5
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz7xyz8

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
3 ________ 16 & nbsp;
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
5
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
6

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp; 
The original string is : Geeksforgeeks is best Computer Science Portal 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 

Làm cách nào để trích xuất một chuỗi sau một ký tự trong Python?
Method #2 : Using regex[ findall[] ] 
In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in string using split can fail and hence requires regular expressions to perform this task. findall function returns the list after filtering the string and extracting words ignoring punctuation marks.
 

Python3

>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
7
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
8

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
0____11
>>> re.findall[r'@[[A-Za-z]+]', '@Hello there @bobv2.0 @!']
['Hello', 'bobv']
1

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
5 ________ 16 & nbsp;
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
7

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
8
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
1 xyz2xyz3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz5
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz7xyz8

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
3 ________ 16 & nbsp;
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
5
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
6

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp; 
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 

& nbsp; & nbsp; Phương thức #3: Sử dụng regex [] + string.punning & nbsp; Phương pháp này cũng sử dụng các biểu thức chính quy, nhưng chức năng chuỗi của tất cả các dấu chấm câu được sử dụng để bỏ qua tất cả các dấu chấm câu và nhận chuỗi kết quả được lọc. & NBSP;
Method #3 : Using regex[] + string.punctuation 
This method also used regular expressions, but string function of getting all the punctuations is used to ignore all the punctuation marks and get the filtered result string.
 

Python3

>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
7
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
8

>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
7 xyz@abc1

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
0____11
>>> re.findall[r'@[[A-Za-z]+]', '@Hello there @bobv2.0 @!']
['Hello', 'bobv']
1

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
5 ________ 16 & nbsp;
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
7

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
8
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
1 xyz2xyz3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz5
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz7xyz8

>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
3 ________ 16 & nbsp;
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
5
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
6

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp; 
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! 
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
 


Làm cách nào để trích xuất một chuỗi sau một ký tự trong Python?

Sử dụng Split [] để có được chuỗi sau khi xuất hiện chuỗi con đã cho.Hàm phân chia cũng có thể được áp dụng để thực hiện nhiệm vụ cụ thể này, trong chức năng này, chúng tôi sử dụng sức mạnh của việc giới hạn phân chia và sau đó in chuỗi sau.. The split function can also be applied to perform this particular task, in this function, we use the power of limiting the split and then print the later string.

Làm cách nào để trích xuất một phần cụ thể của chuỗi trong Python?

Nhận một chuỗi con của một chuỗi đang trích xuất một phần của chuỗi từ một đối tượng chuỗi.Nó cũng được gọi là một hoạt động cắt lát.Bạn có thể nhận được chuỗi con của một chuỗi trong Python bằng tùy chọn STR [0: N].using the str[0:n] option.

Làm thế nào để bạn trích xuất một giá trị từ một chuỗi trong Python?

Tóm tắt: Để trích xuất số từ một chuỗi đã cho trong Python, bạn có thể sử dụng một trong các phương thức sau:..
Sử dụng mô -đun Regex ..
Sử dụng các hàm split [] và append [] trong danh sách ..
Sử dụng danh sách hiểu với các hàm isDigit [] và split [] ..
Sử dụng mô -đun Num_From_String ..

Bài Viết Liên Quan

Chủ Đề