Skip to content Skip to sidebar Skip to footer

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 e

Solution 1:

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.']

Solution 2:

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 :)


Post a Comment for "How To Read The Line That Contains A String Then Extract This Line Without This String"