0
def check():
    with open('cefuse.txt') as f:
        datafile = f.readlines()
    for line in datafile:
        if line starts with 'Word 30' and contains only 0
            return True
    return False 

I want to check a line in .txt file. If it's contains only 0 then retcode will 1 otherwise 0. This is my line: Word 30 : 0x01312e58

  • Does this answer your question? [How to check a string for specific characters?](https://stackoverflow.com/questions/5188792/how-to-check-a-string-for-specific-characters) – Christopher Peisert Oct 15 '20 at 12:47
  • 1
    Your description doesn't match your code snippet. – 0x5453 Oct 15 '20 at 12:48
  • 1
    I suppose you want `if line.startswith('Word 30') and line.split(":")[-1].strip() == '0'`.What does it mean to **contain only 0** ? the line you have also contains `Word 30 :`, do you want to see if it contains something after the `:`? – Countour-Integral Oct 15 '20 at 12:58
  • My line is Word 30 : 0x01312e58. After Word 30 all characters could be changed. I want to check if they will be 0 then it fails otherwise pass. – QA Engineer Oct 15 '20 at 13:07

2 Answers2

1

I hope your are expecting like this.


def check():
    with open('cefuse.txt') as f:
        datafile = f.readlines()
    for line in datafile:
        if line startswith('Word 30') and '0' in line:
            return True
    return False
Z Islam
  • 11
  • 1
  • Like that but line can be "Word 30: 0x000000" this is for me fail but if one of the zeros not zero then it's pass.like 0x00200. – QA Engineer Oct 15 '20 at 13:31
  • I'm not clear with requirements. Could you please show your expected output? Does your line always start with *word 30 * for true condition? – Z Islam Oct 15 '20 at 14:15
0

In addition to the link Christopher Peisert posted, check this one out as well, Find keywords in a text file.

Also, please read the guide on how to ask questions, and actually ask a question. Thanks!

Tyler
  • 143
  • 9