python - how to compare string in one line with string in next line? -
i have file 16,000 lines in it. of them have same format. here simple example if it:
atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 <...> atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00
i need check if lines contains string dppc
, has identifier 18
forms 50 line block before identifier switches 19
, etc.
so now, have following code:
cnt = 0 open('test_file.pdb') f1: open('out','a') f2: lines = f1.readlines() i, line in enumerate(lines): if "dppc" in line: = line.strip()[22:26] if a[i] == [i+1]: cnt = cnt + 1 elif a[i] != a[i+1]: cnt = 0
and here stuck. found examples how compare subsequent lines similar approach did not work here. still cannot figure out how compare value of a
in line[i]
value of a
in line[i+1]
.
try (explanations in comments).
data = """atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 139 c1 dppc 18 17.250 58.420 10.850 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00 atom 189 c1 dppc 19 23.050 20.800 11.000 1.00 0.00""" # last code seen in 5th column. code = none # count of lines of current code. count = 0 line in data.split("\n"): # 5th column. c = line.split()[4] # code in 5th column changed. if c != code: # if aren't @ start of file, print count # code ended. if code: print("{}: {}".format(code, count)) # rember new code. code = c # count line count = count + 1 # print count last code. print("{}: {}".format(code, count))
output:
18: 9 19: 19
Comments
Post a Comment