From f1792c578f9e385ed62045af58d3be4f0cc4a9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20Sillaj=C3=B5e?= Date: Wed, 5 Dec 2018 14:21:59 +0200 Subject: [PATCH] use generator in my_text.py" --- my_text.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 my_text.py diff --git a/my_text.py b/my_text.py new file mode 100644 index 0000000..d9ac87f --- /dev/null +++ b/my_text.py @@ -0,0 +1,28 @@ +import re +from itertools import islice +""" +Read the text file to a list mylist where eache item is a row. + +1. Find the start index and end index where lines must be extracted. +2. + [index1, index2,.. indexn] + + new_list_1: mylist[:index1] + new_list_2: mylist[index1:index2] + ... + new_list_3: mylist[indexn:] +""" + +p = re.compile(r'^.*ladu2.*$') + +indexes = [] + +def lines_generator(placeholder=0): + with open("tabel.txt", "rt") as f: + for line in islice(f,0,6 ): + yield(line) + + +placeholder=0 +mylines = lines_generator() +print(next(mylines))