Poker Prof
Poker Prof

Reputation: 169

Python Detect Missing Line in a Txt File

Let us say a txt file consists of these lines:

A
  1
  2
  3
B 
  1
  2
  3
C
  1
  3

Clearly, 2 is missing from C in the txt file. What is the idea to detect the missing 2 and output it? I need to read the txt file line by line.

Thank you !

Upvotes: -2

Views: 471

Answers (2)

simplylizz
simplylizz

Reputation: 1704

Probably you want something like this:

line_sets = []
file_names = ['a', 'b', 'c']

# read content of files, if you need to remember file names, use
# dict instead of list
for file_name in file_names:
    with open(file_name, 'rb') as f:
        line_sets.append(set(f.readlines()))

# find missing lines
missing_lines = set()
for first_set, second_set in itertools.combinations(line_sets, 2):
    missing_lines.add(first_set - second_set)
print 'Missing lines: %s' % missing_lines

Upvotes: 0

Poker Prof
Poker Prof

Reputation: 169

Ok I think the question is not clear to most of you. Anyway here is my solution:

I append the values from each section into a list inside a for loop. For example, in section A, the list will contains 1,2 and 3. The len of the list in section C will be only 2. Thus, we know that a value is missing from section C. From here, we can print out section C. Sorry for the misunderstandings. This question is officially close. Thanks for the view anyway!

Upvotes: -1

Related Questions