Maimon、インデックスに関して最初のコードが間違っています。Andrew のコードも間違っています。
gに関する行を削除したアンドリューのコードの結果を参照してください。
import sys
import operator
import linecache
x=0
y=7 # to skip
z=3 # to print
#g = open('tmp','a+')
while x<23:
x+=1
print 'x==',x
if operator.mod(x, y)==0:
#curr = g.tell()
for i in range(x,x+z):
block=linecache.getline('poem.txt', i)
print 'block==',repr(block)
#g.write(block)
linecache.clearcache()
#g.seek(curr)
#lines = g.read()
#find(lines)
else:
pass
#g.close()
24 行を含む「poem.txt」という名前のファイルに適用されます。
1 In such a night, when every louder wind
2 Is to its distant cavern safe confined;
3 And only gentle Zephyr fans his wings,
4 And lonely Philomel, still waking, sings;
5 Or from some tree, famed for the owl's delight,
6 She, hollowing clear, directs the wand'rer right:
7 In such a night, when passing clouds give place,
8 Or thinly veil the heav'ns' mysterious face;
9 When in some river, overhung with green,
10 The waving moon and trembling leaves are seen;
11 When freshened grass now bears itself upright,
12 And makes cool banks to pleasing rest invite,
13 Whence springs the woodbind, and the bramble-rose,
14 And where the sleepy cowslip sheltered grows;
15 Whilst now a paler hue the foxglove takes,
16 Yet checkers still with red the dusky brakes
17 When scattered glow-worms, but in twilight fine,
18 Shew trivial beauties watch their hour to shine;
19 Whilst Salisb'ry stands the test of every light,
20 In perfect charms, and perfect virtue bright:
21 When odors, which declined repelling day,
22 Through temp'rate air uninterrupted stray;
23 When darkened groves their softest shadows wear,
24 And falling waters we distinctly hear;
結果は次のとおりです。
x== 1
x== 2
x== 3
x== 4
x== 5
x== 6
x== 7
block== '7 In such a night, when passing clouds give place,\n'
block== "8 Or thinly veil the heav'ns' mysterious face;\n"
block== '9 When in some river, overhung with green,\n'
x== 8
x== 9
x== 10
x== 11
x== 12
x== 13
x== 14
block== '14 And where the sleepy cowslip sheltered grows;\n'
block== '15 Whilst now a paler hue the foxglove takes,\n'
block== '16 Yet checkers still with red the dusky brakes\n'
x== 15
x== 16
x== 17
x== 18
x== 19
x== 20
x== 21
block== '21 When odors, which declined repelling day,\n'
block== "22 Through temp'rate air uninterrupted stray;\n"
block== '23 When darkened groves their softest shadows wear,\n'
x== 22
x== 23
x== 24
x== 25
スキップする行数に y=7 を選択しましたが、7 行目が印刷されます。
また、カウントは、10、11、12 で続行する代わりに、3 行 7-8-9 (z=3 を選択) を印刷した後、8、9、10... で続行されます。次に、次の 3 行が印刷されます。 14-15-16 ですが、最初の 7 + 3 行の後の行、つまり行 11-12-13 である必要があります。
実際、7 行をスキップして 3 行を印刷する場合、印刷される行は次のようになります:
8-9-10
18-19-20
28-29-30
など
私は正しいですか?
編集1
私の解決策は次のとおりです。
def chunk_reading(filepath,y,z,x=0):
# x : number of lines to skip before the periodical treatment
# y : number of lines to periodically skip
# z : number of lines to periodically print
with open('poem.txt') as f:
try:
for sk in xrange(x):
f.next()
while True:
try:
for i in xrange(y):
print 'i==',i
f.next()
for j in xrange(z):
print 'j==',j
print repr(f.next())
except StopIteration:
break
except StopIteration:
print 'Not enough lines before the lines to print'
chunk_reading('poem.txt',7,3)
生成:
i== 0
i== 1
i== 2
i== 3
i== 4
i== 5
i== 6
j== 0
"8 Or thinly veil the heav'ns' mysterious face;\n"
j== 1
'9 When in some river, overhung with green,\n'
j== 2
'10 The waving moon and trembling leaves are seen;\n'
i== 0
i== 1
i== 2
i== 3
i== 4
i== 5
i== 6
j== 0
'18 Shew trivial beauties watch their hour to shine;\n'
j== 1
"19 Whilst Salisb'ry stands the test of every light,\n"
j== 2
'20 In perfect charms, and perfect virtue bright:\n'
i== 0
i== 1
i== 2
i== 3
i== 4
編集2
上記のソリューションは、RAM に記録できない非常に大きなファイルにも使用できます。
次のものは、サイズが制限されたファイルに使用できます。
def slice_reading(filepath,y,z,x=0):
# x : number of lines to skip before the periodical treatment
# y : number of lines to periodically skip
# z : number of lines to periodically print
with open('poem.txt') as f:
lines = f.readlines()
lgth = len(lines)
if lgth > x+y:
for a in xrange(x+y,lgth,y+z):
print lines[a:a+z]
else:
print 'Not enough lines before lines to print'
slice_reading('poem.txt',7,3,5)
結果
['13 Whence springs the woodbind, and the bramble-rose,\n', '14 And where the sleepy cowslip sheltered grows;\n', '15 Whilst now a paler hue the foxglove takes,\n']
['23 When darkened groves their softest shadows wear,\n', '24 And falling waters we distinctly hear;']