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

2024/10/14 8:28:54

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, M.A.") can I also extract the last name and first name. So the final results will be:

 Python and Matplotlib Essentials for Scientists and EngineersWoodM.A.
Answer

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.']
https://en.xdnf.cn/q/117976.html

Related Q&A

Python: How to access and iterate over a list of div class element using (BeautifulSoup)

Im parsing data about car production with BeautifulSoup (see also my first question):from bs4 import BeautifulSoup import stringhtml = """ <h4>Production Capacity (year)</h4>…

What should I worry about Python template engines and web frameworks? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Value Search from Dictionary via User Input

I have written the following code for getting an output of the various districts located in the given city and their respective postal codes. I want my code to be able to receive input from the user (D…

Read and aggregate data from CSV file

I have a data file with the following format:name,cost1,cost1,cost1,cost2,cost3,cost3, X,2,4,6,5,6,8, Y,0,3,6,5,4,6, . . ....Now, what I would like to do is to convert this to a dictionary of dictionar…

nltk cant using ImportError: cannot import name compat

This is my codeimport nltk freq_dist = nltk.FreqDist(words) print freq_dist.keys()[:50] # 50 most frequent tokens print freq_dist.keys()[-50:] # 50 least frequent tokensAnd I am getting this error mess…

Fitting and Plotting Lognormal

Im having trouble doing something as relatively simple as:Draw N samples from a gaussian with some mean and variance Take logs to those N samples Fit a lognormal (using stats.lognorm.fit) Spit out a n…

Is there any way to install nose in Maya?

Im using Autodesk Maya 2008 on Linux at home, and Maya 2012 on Windows 7 at work. Most of my efforts so far have been focused on the former. I found this thread, and managed to get the setup there work…

Basic python socket server application doesnt result in expected output

Im trying to write a basic server / client application in python, where the clients sends the numbers 1-15 to the server, and the server prints it on the server side console. Code for client:import soc…

creating dictionaries to list order of ranking

I have a list of people and who controls who but I need to combine them all and form several sentences to compute which person control a list of people.The employee order comes from a txt file:

Python: How to use MFdataset in netCDF4

I am trying to read multiple NetCDF files and my code returns the error:ValueError: MFNetCDF4 only works with NETCDF3_* and NETCDF4_CLASSIC formatted files, not NETCDF4. I looked up the documentation a…