1

I have a text file with given below content

Credit

Debit

21/12/2017

09:10:00

Written python code to convert text into set and discard \n.

with open('text_file_name', 'r') as file1:
    same = set(file1)
    print (same)
    print (same.discard('\n'))

for first print statement print (same). I get correct result:

{'Credit\n','Debit\n','21/12/2017\n','09:10:00\n'}

But for second print statement print (same.discard('\n')) . I am getting result as None.

Can anybody help me to figure out why I am getting None. I am using same.discard('\n') to discard \n in the set.

Note: I am trying to understand the discard function with respect to set.

Kandan Siva
  • 471
  • 2
  • 9
  • 20

3 Answers3

1

The discard method will only remove an element from the set, since your set doesn't contain just \n it can't discard it. What you are looking for is a map that strips the \n from each element like so:

set(map(lambda x: x.rstrip('\n'), same))

which will return {'Credit', 'Debit', '09:10:00', '21/12/2017'} as the set. This sample works by using the map builtin which applies it's first argument to each element in the set. The first argument in our map usage is lambda x: x.rstrip('\n') which is simply going to remove any occurrences of \n on the right-hand side of each string.

mattjegan
  • 2,724
  • 1
  • 26
  • 37
  • 1
    I think this would be the most memory & time efficient answer because the builtin map method is implemented in C. – A. J. Parr Dec 21 '17 at 01:31
0

discard removes the given element from the set only if it presents in it. In addition, the function doesn't return any value as it changes the set it was ran from.

with open('text_file_name', 'r') as file1:
    same = set(file1)
    print (same)
    same = {elem[:len(elem) - 1] for elem in same if elem.endswith('\n')}
    print (same)
DjLegolas
  • 76
  • 1
  • 4
-1

There are 4 elements in the set, and none of them are newline.

It would be more usual to use a list in this case, as that preserves order while a set is not guaranteed to preserve order, plus it discards duplicate lines. Perhaps you have your reasons.

You seem to be looking for rstrip('\n'). Consider processing the file in this way:

    s = {}
    with open('text_file_name') as file1:
        for line in file1:
            s.add(line.rstrip('\n'))
    s.discard('Credit')
    print(s) # This displays 3 elements, without trailing newlines.
J_H
  • 17,926
  • 4
  • 24
  • 44
  • Even if there WAS a newline as an element of the set, you would still get `None` as the result from the `.discard()` method. Its job is to remove an element from an existing set, it doesn't need to return anything since you already have the set. – jasonharper Dec 21 '17 at 01:19
  • 1
    It appeared to me the OP was confused about how far into the collection `discard` would reach, so I tried to answer the real question ("how do I strip newlines?") rather than the one that literally appeared in the posting. I was trying to be helpful. – J_H Dec 21 '17 at 01:25