-1

I have a python list in which look like this:

my_list = ['OFAC\n', 'Social Media Analytics\n', 'Teaching Skills\n', 'Territory...\n', 'Active Directory...\n', 'Business Research\n', 'Call Center...\n', 'Treatment of depression\n', 'VB\n', 'CAN\n', 'Client Interfacing...\n', 'Consolidated Financial...\n']

How can i remove the \n from the end of each element and why is strip not working ?.

I tried line.strip('\n') and line.replace('\n','') but it is not affecting anything.

The text file from which the list is made looks like this:

Java Basic
Core Java
C++
Python
...

Code for putting the textfile into the list is:

with open("myfile.txt") as f:
    for line in f:
        st_line = line.strip('\n')
        d.append(st_line)
anshaj
  • 293
  • 1
  • 6
  • 24

1 Answers1

1

Try something like this:

with open("myfile.txt") as f:
    d = map(str.rstrip, f.readlines())
print d

For your input above this will output:

['Java Basic', 'Core Java', 'C++', 'Python']
Szabolcs
  • 3,990
  • 18
  • 38