Friday 15 July 2011

python - check if any of the items of a list is in a line of a file, if not, then write the line in a new file -


I have a file that has many lines and I need to create a new file, in which there are some words that line except .

has created a code that is working but there are so many words, therefore, it would be better to put these words in one list and verify the contents of that list. After code:

  infile = file ('. / Infile_test.txt') for newopen = open ('. / Newfile.txt', 'w') in the infile: if ' Ssh 'not in line and' SNMP 'is not in line and' etc. 'is not in line: newopen.write (line)  

This is an example, but assuming that infile_test The .txt lines were below, except for the 2,4 and 6 lines, a new file will be created:

  Line 1: This is a file test line 2: SSH, SNAP line 3: This idea is repeating Each of this file line is Row 4: If the list of any words in row (SSI, etc.) does not appear 5: row have been deleted a file line 6: etc line Likiaः Itens 7 or not?  

I believe that by creating a list:

  list = ['ssh', 'snmp', 'etc']  

And then by comparing each list it may be better to run it again, has tried to make a new "attempt" to use the "all" and "any" function, but She was not working well

  infile = File ('. / Infile_test.txt') outfile = open ('. / Newfile.txt', 'w') ignore_list = ['SSH', 'SNMP', 'etc.'] Rows in the file For: if not, for word in word in ignore_list): outfile.write (line)  

No comments:

Post a Comment