Hướng dẫn how do you extract a specific word from a line in python? - làm thế nào để bạn trích xuất một từ cụ thể từ một dòng trong python?

Đô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

test_string = "Geeksforgeeks is best Computer Science Portal"

DR   ac
FT   ae
//
DR   cc
FT   ce
//
0
DR   ac
FT   ae
//
DR   cc
FT   ce
//
1
DR   ac
FT   ae
//
DR   cc
FT   ce
//
2 ________ 13 & nbsp;
DR   ac
FT   ae
//
DR   cc
FT   ce
//
4

DR   ac
FT   ae
//
DR   cc
FT   ce
//
5=
DR   ac
FT   ae
//
DR   cc
FT   ce
//
7

DR   ac
FT   ae
//
DR   cc
FT   ce
//
0
DR   ac
FT   ae
//
DR   cc
FT   ce
//
1
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
0 ________ 13 & nbsp;
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
2
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
3

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek là cổng thông tin khoa học máy tính tốt nhất & nbsp; danh sách các từ là: [' ; 
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 pháp #2: Sử dụng regex [findall []] & nbsp; Trong các trường hợp chứa tất cả các ký tự đặc biệt và dấu chấm câu, như đã thảo luận ở trên, phương pháp tìm kiếm thông thường Biểu thức để thực hiện nhiệm vụ này. Hàm Findall Trả về danh sách sau khi lọc chuỗi và trích xuất các từ bỏ qua các dấu chấm câu. & nbsp;
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

word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
4
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
5

test_string =

word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
8

DR   ac
FT   ae
//
DR   cc
FT   ce
//
0
DR   ac
FT   ae
//
DR   cc
FT   ce
//
1
DR   ac
FT   ae
//
DR   cc
FT   ce
//
2 ________ 13 & nbsp;
DR   ac
FT   ae
//
DR   cc
FT   ce
//
4

DR   ac
FT   ae
//
DR   cc
FT   ce
//
5=
DR   ac
FT   ae
//
DR   cc
FT   ce
//
7

DR   ac
FT   ae
//
DR   cc
FT   ce
//
0
DR   ac
FT   ae
//
DR   cc
FT   ce
//
1
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
0 ________ 13 & nbsp;
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
2
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
3

Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek là cổng thông tin khoa học máy tính tốt nhất & nbsp; danh sách các từ là: [' ; 
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 pháp #2: Sử dụng regex [findall []] & nbsp; Trong các trường hợp chứa tất cả các ký tự đặc biệt và dấu chấm câu, như đã thảo luận ở trên, phương pháp tìm kiếm thông thường Biểu thức để thực hiện nhiệm vụ này. Hàm Findall Trả về danh sách sau khi lọc chuỗi và trích xuất các từ bỏ qua các dấu chấm câu. & 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

word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
4
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
5

test_string =

word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
8

test_string =

word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
8

DR   ac
FT   ae
//
DR   cc
FT   ce
//
0
DR   ac
FT   ae
//
DR   cc
FT   ce
//
1
DR   ac
FT   ae
//
DR   cc
FT   ce
//
2 ________ 13 & nbsp;
DR   ac
FT   ae
//
DR   cc
FT   ce
//
4

DR   ac
FT   ae
//
DR   cc
FT   ce
//
5=
DR   ac
FT   ae
//
DR   cc
FT   ce
//
7

DR   ac
FT   ae
//
DR   cc
FT   ce
//
0
DR   ac
FT   ae
//
DR   cc
FT   ce
//
1
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
0 ________ 13 & nbsp;
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
2
word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line
3

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


1

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Input:

ID   aa
AA   Homo sapiens
DR   ac
BB   ad
FT   ae
//
ID   ba
AA   mouse
DR   bc
BB   bd
FT   be
//
ID   ca
AA   Homo sapiens
DR   cc
BB   cd
FT   ce
//

Đầu ra dự kiến:

DR   ac
FT   ae
//
DR   cc
FT   ce
//

Code:

word = 'Homo sapiens'
with open[input_file, 'r'] as txtin, open[output_file, 'w'] as txtout:
    
    for block in txtin.read[].split['//\n']:   # reading a file in blocks
        if word in block:   # extracted block containing the word, 'Homo sapiens'
            extracted_block = block + '//\n'

            for line in extracted_block.strip[].split['\n']:   # divide each block into lines
                if line.startswith['DR   ']:
                    dr = line 

                elif line.startswith['FT   ']:
                    ft = line

Tôi đọc input_file dựa trên '//' [khối]. Và, nếu từ 'Homo sapiens' được bao gồm trong các khối, tôi đã trích xuất các khối. Ngoài ra, trong khối, dòng bắt đầu bằng 'DR' được định nghĩa là DR và ​​dòng bắt đầu bằng 'ft' được định nghĩa là ft. Tôi nên viết 'đầu ra' bằng cách sử dụng DR và ​​FT để có được 'đầu ra dự kiến'?

Hỏi ngày 21 tháng 12 năm 2021 lúc 1:40Dec 21, 2021 at 1:40

Nếu bạn mở cho giải pháp dựa trên Regex, thì một tùy chọn sẽ là đọc toàn bộ tệp thành một chuỗi và sau đó sử dụng test_string 2:

with open[input_file, 'r'] as file:
    inp = file.read[]

matches = re.findall[r'[?

Bài Viết Liên Quan

Chủ Đề