I just wrote a function that will look at a text file and count all of the instances of True and False in the text file. Here is my file
ATOM 43 CA LYS A 5 14.038 15.691 37.608 1.00 15.15 C True
ATOM 52 CA CYS A 6 16.184 12.782 38.807 1.00 16.72 C True
ATOM 58 CA GLU A 7 17.496 12.053 35.319 1.00 14.06 C False
ATOM 67 CA VAL A 8 18.375 15.721 34.871 1.00 12.27 C True
ATOM 74 CA PHE A 9 20.066 15.836 38.288 1.00 12.13 C False
ATOM 85 CA GLN A 10 22.355 12.978 37.249 1.00 12.54 C False
And here is my code
def TFCount(txtFileName):
with open(txtFileName, 'r') as e:
T = 0
F = 0
for record in e:
if(re.search(r'^ATOM\s+\d+\s+\CA\s+\w+\s+\w+\s+\d+\s+\d+\.\d+\s+\d+\.\d+\s+\d+\.\d+\s+\d+\.\d+\s+\d+\.\d+\s+\w+\s+\T', record)):
T += 1
else:
F += 1
print(T)
print(F)
I apologize if my regex is long and tedious to read, but this is the only way I know of counting the number of times True occurs in the file. As you can see, each time the program encounters True, it will add 1 to the variable T, otherwise it will add 1 to the variable False. After attempting to run the program, the interpreter returns error: bad escape \C. What does this error mean? And what in my code is causing it?
C? There is no\Cregex escape. Beginning with Python 3.6, this is treated as an error. Before, it would have been parsed as aC. It looks like a typo on your part. - Wiktor Stribiżew\C. Most engines will just unescape it as a pass, but apparently not Python. Or, it's probably a message from the interpreter. - user557597'^ATOM.{80}T'would work just as well and be a lot easier to understand. - Mark Ransomsubstrand equalities would do the trick without having to rely on regexps. Extract columns 1-6 and compare againstATOM, extract columns 13-16 and compare against ` CA. If it is the expected line, check for theT` in its own column (wwpdb.org/documentation/file-format-content/format33/…). - Poshi