简体   繁体   中英

How to read the line that contains a string then extract this line without this string

I have a file .txt that contains a specific line, like this

file.txt

.
.
T - Python and Matplotlib Essentials for Scientists and Engineers
.
A - Wood, M.A.
.
.
.

I would like to extract lines that contain a string, I tried with a simple script:

with open('file.txt','r') as f:
    for line in f:
        if "T - " in line:
            o_t = line.rstrip('\n')
        elif "A - " in line:
            o_a = line.rstrip('\n')


o_T = o_t.split('T - ')
print (o_T)

o_A = o_a.split('A - ')
#o_Fname =
#o_Lname =
print (o_A)

my output:

['', 'Python and Matplotlib Essentials for Scientists and Engineers']
['', 'Wood, M.A.']

and my desired output:

Python and Matplotlib Essentials for Scientists and Engineers
Wood, M.A.

moreover, for the second ("Wood, MA") can I also extract the last name and first name. So the final results will be:

 Python and Matplotlib Essentials for Scientists and Engineers
 Wood
 M.A.

Use filter to remove all empty elements from list.

Ex:

o_T = filter(None, o_t.split('T - '))
print (o_T)
o_A = filter(None, o_a.split('A - '))
print (o_A)

Output:

['Python and Matplotlib Essentials for Scientists and Engineers']
['Wood, M.A.']

The fault in your case is that you print o_t instead of o_T (which is the result of the split operation).

However as others pointed out you could also approach this by removing the first 4 characters, by using regex \\w - (.+) , then you could get all values. If you also need the first character, you could use (\\w) - (.+) .

In addition to that, if you'd give your variables better names, you'd have a better life :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM