Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I only print every 5th line

I have a text file ("name_data.txt") that has the following contents:

name: Kelo
family name: Lam
location: Asia
members: Kelo, Kiko, Jil

name: Miko
family name: Naiton
location: Japan
members: Miko,Kayati 

The text file keeps going with the same pattern (name, family name, location, members)

I want to print out the first line and then print every 5th line so I would be printing only the line with "name" in the beginning. I then want to have a list of the names

I want my output to be :

["Kelo","Miko"]

So far, I have gotten (although, it is wrong):

name_data= load_local_file('name_data.txt',ignore_header=False,delimiter='\t')


def __init __(name_reader): 

    names=list()  
    count=0  
    name_line=5  
    line_number=0  

    for name in name_data:

        if line_number<5:  

            line_number +=1  

        if line_number ==5: 

            names.append(line_number)  
like image 929
Robbie Avatar asked Dec 11 '16 02:12

Robbie


People also ask

How do I print every second line in Linux?

Using sed:n command in sed prints the current line(if -n is not present) and reads the next line in the buffer. Since -n switch is present, the n command does not print.

How do I print multiple rows?

You can select multiple rows by clicking on the first row and dragging to select a range of rows. To get one or more columns to print on the left side of each page, click in the Columns to repeat at left box, then click on the column or columns you want to have printed on each page.


Video Answer


1 Answers

You can identify every fifth line by comparing the linenumber modulo 5 against a number. In your case this should be 0 because you want the first line and the 6th, the 11th, ... (note that python starts with index 0)

To get the line-numbers as well as the content you can iterate over the file with enumerate.

Then to discard the name: part of the string and keep what comes after, you can use str.split().

A working implementation could look like this:

# Create an empty list for the names
names = []

# Opening the file with "with" makes sure it is automatically closed even
# if the program encounters an Exception.
with open('name_data.txt', 'r') as file:
    for lineno, line in enumerate(file):
        # The lineno modulo 5 is zero for the first line and every fifth line thereafter.
        if lineno % 5 == 0:
            # Make sure it really starts with "name"
            if not line.startswith('name'):
                raise ValueError('line did not start with "name".')
            # Split the line by the ":" and keep only what is coming after it.
            # Using `maxsplit=1` makes sure you don't run into trouble if the name 
            # contains ":" as well (may be unnecessary but better safe than sorry!)
            name = line.split(':', 1)[1]
            # Remove any remaining whitespaces around the name
            name = name.strip()
            # Save the name in the list of names
            names.append(name)

# print out the list of names
print(names)

Instead of enumerate you could also use itertools.islice with a step argument:

from itertools import islice

with open('name_data.txt', 'r') as file:
    for line in islice(file, None, None, 5):
        ... # like above except for the "if lineno % 5 == 0:" line

Depending on your needs you might consider using the re module to completly parse the file:

import re
# The regular expression
group = re.compile(r"name: (.+)\nfamily name: (.+)\nlocation: (.+)\nmembers: (.+)\n", flags=re.MULTILINE)
with open(filename, 'r') as file:
    # Apply the regex to your file
    all_data = re.findall(group, file)
# To get the names you just need the first element in each group:
firstnames = [item[0] for item in all_data]

The firstnames will be ['Kelo', 'Miko'] for your example and similar if you use [item[1] for item in all_data] then you get the last names: ['Lam', 'Naiton']. To successfully use a regular expression you must ensure it really matches your file layout otherwise you'll get wrong results.

like image 70
MSeifert Avatar answered Oct 14 '22 18:10

MSeifert