You can identify every fifth line by comparing linenumber modulo 5
with a number. In your case, it should be 0
, because you need the first line and the 6th, 11th, ... (note that python starts at index 0)
To get line numbers, as well as content that you can iterate over a file using enumerate
.
Then, to discard part of the name:
and save what happens after that, you can use str.split()
.
A working implementation might look like this:
Instead of listing, you can also use itertools.islice
with a step:
from itertools import islice with open('name_data.txt', 'r') as file: for line in islice(file, None, None, 5): ...
Depending on your needs, you can use the re
module to fully analyze the file:
import re
firstnames
will be ['Kelo', 'Miko']
for your example and similar, if you use [item[1] for item in all_data]
then you will get the last names: ['Lam', 'Naiton']
. To successfully use the regular expression, you must make sure that it really matches your file layout, otherwise you will get incorrect results.
source share