我需要在特定行之後讀取文本文件,假定行號爲100.此行具有特定的編號,例如'255'。然後我想讀取下500行使用for循環。在這500行中,我有一些數字要提取。如在P [3]的位置。然後我需要將這些值傳遞給一個數組。最後,我應該有如下幾組。我用下面的代碼來做到這一點。但我失敗了。誰能幫我。需要閱讀Python中特定範圍的文本文件
文件看起來像下面
Generated by trjconv : a bunch of waters t= 0.00000
500
1SOL OW 1 1.5040 2.7580 0.6820
2SOL OW 4 1.5210 0.9510 2.2050
500SOL OW 2998 1.5310 1.7952 2.1981
3.12736 3.12736 3.12736
Generated by trjconv : a bunch of waters t= 9000.00000
500
1SOL OW 1 1.5040 2.7580 0.6820
2SOL OW 4 1.5210 0.9510 2.2050
500SOL OW 2998 1.5310 1.7952 2.1981
3.10941 3.10941 3.10941
Generated by trjconv : a bunch of waters t= 0.00000
500
1SOL OW 1 1.5040 2.7580 0.6820
2SOL OW 4 1.5210 0.9510 2.2050
500SOL OW 2998 1.5310 1.7952 2.1981
3.12736 3.12736 3.12736
Generated by trjconv : a bunch of waters t= 9000.00000
500
1SOL OW 1 1.5040 2.7580 0.6820
2SOL OW 4 1.5210 0.9510 2.2050
500SOL OW 2998 1.5310 1.7952 2.1981
3.10941 3.10941 3.10941
編碼我已經寫
F = open('Data.gro', 'r')
A = open('XYZ.txt', 'w')
XO = []
I = range(1,500)
for line in F:
P = line.split()
if P[0] == '256': # after i found this I want to read next five hundred lines.
for R in I:
P = line.split()
XO.append(P[3])
R +=1
# after the for loop I want write 'XO' in to file as set 01 then should go to next P[0] == '256'
結果應該像下面的文件名 'xyz.txt將該'
Set 01
X = [1.32, 4.132, 2.23, .... upto 500]
Set 02
X = [4.232, 1.162, 3.73, .... upto 500]
難道你不想在每個500循環之後將'XO'重置爲'[]'嗎? –