に次のコードを考えてみましょう:不安定(シーク(一見ランダムな)行動)と分割()のpython
import sys
with open(sys.argv[1]) as data_file:
data_file.readline() #skipping lines of texts
data_file.readline()
data_file.readline() #skipping lines of texts
data_file.readline()
data_file.readline() #skipping lines of texts
data_file.readline() #skipping lines of texts
data_file.readline() #skipping lines of texts
data_file.readline() #skipping lines of texts
data_file.readline() #skipping lines of texts
while True:
print "#"
pos=data_file.tell()
next_mol=data_file.readline().split()
print next_mol
data_file.seek(pos)
print data_file.readline().split()
は、ここではsys.argvの[1]次のデータを含むテキストファイルの名前です。 :
#
['1', '1', '8.07271', '20.6394', '38.953']
['71', '20.6394', '38.953']
#
['2', '1', '7.45444', '20.2706', '37.5682']
['1', '7.45444', '20.2706', '37.5682']
#
['3', '1', '7.94593', '21.3438', '36.5822']
['1', '7.94593', '21.3438', '36.5822']
#
['4', '2', '8.88701', '22.2414', '37.422']
['2', '8.88701', '22.2414', '37.422']
#
['5', '6', '8.97587', '21.7898', '38.6976']
['6', '8.97587', '21.7898', '38.6976']
#
['6', '7', '9.51512', '23.1098', '36.8675']
['7', '9.51512', '23.1098', '36.8675']
#
['7', '1', '9.83459', '22.2787', '39.7728']
['1', '9.83459', '22.2787', '39.7728']
#
['8', '3', '8.54346', '19.7726', '39.3733']
['3', '8.54346', '19.7726', '39.3733']
#
['9', '3', '7.3188', '20.9572', '39.6053']
['3', '7.3188', '20.9572', '39.6053']
#
['10', '3', '6.33686', '20.2798', '37.6457']
['0', '3', '6.33686', '20.2798', '37.6457']
は、私が同じになるように '#' の間の両方の文字列を期待していた。
ITEM: TIMESTEP
31500000
ITEM: NUMBER OF ATOMS
28244
ITEM: BOX BOUNDS pp pp pp
0.706774 63.6072
1.77317 62.6918
-4.27518 67.4572
ITEM: ATOMS id type x y z
1 1 8.07271 20.6394 38.953
2 1 7.45444 20.2706 37.5682
3 1 7.94593 21.3438 36.5822
4 2 8.88701 22.2414 37.422
5 6 8.97587 21.7898 38.6976
6 7 9.51512 23.1098 36.8675
7 1 9.83459 22.2787 39.7728
8 3 8.54346 19.7726 39.3733
9 3 7.3188 20.9572 39.6053
10 3 6.33686 20.2798 37.6457
11 3 7.62824 19.2464 37.1935
12 3 7.14438 21.9616 36.2781
13 3 8.4454 20.9589 35.6742
14 3 9.51704 23.2023 40.2712
15 3 10.839 22.4705 39.342
16 3 9.84061 21.5031 40.5668
は私に次のような出力を提供します。ここに何かがないのですか?
'file.readline()'はほぼ確実にバッファを使います。ファイルの位置は最後に生成された改行の直後ではなく、改行を見つけるために先読みバッファの後に置かれます。 –
より良い代替品はありますか?または次の行を読み、前の位置に戻すための選択肢? – ipcamit
なぜそれをしたいですか?なぜ、すでに読み込まれた行を代わりに保存しないのですか? –