简体   繁体   中英

How to input an specific amount of lines from a text file into an array and repeat the action untill all lines are used

I have a text file with 89000 lines. What I want is to input 100 names at the time into a function, but do this for the entire file.

So you could say:

Load 100 lines

Do something with those lines

Sleep

Repeat

Before this I only used for every line in file but I have no clue how to do; for every 100 lines in file and actually use those lines to read information.

Using itertools.islice , you can get selected items from an iterable:

with open('/path/to/file') as f:
    while True:
        lines = list(itertools.islice(f, 100))  # similar to `f[0:100]`
        if not lines:
            break
        # process lines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM