Home>

Good afternoon, you need to search for a word in a voluminous txt. file, but the result should include both the line with the search word and the next three lines.

This script displays only one line with a word:

with open ("dz.txt") as openfile:
for line in openfile:
    for part in line.split ():
        if "kotik" in part:
            print (part)

It can be seen that in the search console only the first line was displayed, but everything is needed.

@Sasha, where is it not clear? ..

Jack_oS2021-02-23 18:29:34

So, apparently, it’s impossible to stay after lessons and do the work quickly, I’ll start by saying that I just started learning programming and decided to help myself with this work, thanks for the answer, but I didn’t understand anything, I thought everything would look like one script, early in jungle climbed, thanks for the answer again, you are beautiful

Саша2021-02-23 18:29:34

Added izvesiya,) that is, in the results I have to get all three lines by driving only algebra into the search

Саша2021-02-23 18:29:34

I believe) but but drawing is not the next 3 lines. it will be great if you add example input and output data

5c0rp2021-02-23 18:29:34

add example text file

5c0rp2021-02-23 18:29:34
  • Answer # 1

    Save the lines of the file in listlessons_list , for example:

    with open ('dz.txt') as f:
        lessons_list= [line.strip () for line in f.readlines ()]
    

    using list comprehensionand immediately clearing lines from line feed characters.

    Compare the search word with each element in numbered list... If you find a match, print slice of the listlength 3, starting from the found one:

    def search (lessons_list, search):
        for i, lesson in enumerate (lessons_list):
            if search in lesson:
                print (lessons_list [i: i + 3])
    

    For a list:

    lessons_list= ['geometry', 'algebra', 'physics', 'art', 'english', 'biology']
    

    Conclusion:

    >
    >
    >
     search (lessons_list, 'algebra')
    ['algebra', 'physics', 'art']
    >
    >
    >
     search (lessons_list, 'english')
    ['english', 'biology']
    >
    >
    >
     search (lessons_list, 'blablalesson')
    

    Or, if you just need a columnar output, replace print with print (* lessons_list [i: i + 3], sep= '\ n') , then :

    >
    >
    >
     search (lessons_list, 'geometry')
    geometry
    algebra
    physics
    

    In the latter case, it will output nothing, since Nothing found.

    PS The beauty of slices is that they don't throw exceptions;)

    UPD is a simple, non-generic, boring script that "only looks for one line":

    with open ('dz.txt') as f:
        lines_list= [line.strip () for line in f.readlines ()]
        for i, line in enumerate (lines_list):
            if 'algebra' in line.lower ():
                print (* lines_list [i: i + 3], sep= '\ n')
    

    For a file with this content:

    School life is not only about exact sciences,
    such as algebra, geometry, physics or chemistry,
    and not only humanitarian (geography, literature),
    but also our favorite drawing, labor education,
    as well as optional: rhetoric and world culture.
    

    will output:

    such as algebra, geometry, physics or chemistry,
    and not only humanitarian (geography, literature),
    but also our favorite drawing, labor education,
    

    "does not work" will not say anything ... @Sasha, what does he write in the trace?

    Jack_oS2021-02-23 18:29:34

    yes, exactly, but it does not work for me (

    Саша2021-02-23 18:29:34

    @Sasha is it now?

    Jack_oS2021-02-23 18:29:34

    yes, you can, added ... clarifications will be needed -ask

    Jack_oS2021-02-23 18:29:34

    All the same, tell me, these are all different scripts or one broken one, sorry for the lameness, I really want to figure out if it can be brought to a single block like the one I gave in the example that looks for a word and gives out only one line, but you need three, the first with the desired one word and two subsequent, thank you

    Саша2021-02-23 18:29:34
Trends