Remove \n from each string stored in a python list

2024/11/19 3:36:05

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)
Answer

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']
https://en.xdnf.cn/q/118595.html

Related Q&A

Optimizing loop. Faster ResultList.append( [ c, d, c[1]/d[1]] )? Array? Map?

The following works well but Id like to make it faster. The actual application could process Tuple1 and Tuple2 each with 30,000 elements and 17 nested sequences per element. I see numerous questions …

Why do I get an error name play is not defined when I think it is?

Full error: line 10, in <module>colour = play() NameError: name play is not definedI cant seem to find a reason for this issue anywhere on here. I am trying to assign the returned string to the v…

Error: unhashable type: dict

i have a problem with Django: I cant show the data from mysql database in the table. I see the error "Exception Value: unhashable type: dict" This is my code: views.py:List_of_date=El.objects…

terminal command line python3.3

Im following a book tutorial and its telling me to install python3.3 with the command linesudo apt-get install python3.3however Im getting errorsUnable to locate package python3.3 Couldnt find any pack…

SQLalchemy making errors after being updated to 1.4.0 [duplicate]

This question already has answers here:ImportError: cannot import name _ColumnEntity from sqlalchemy.orm.query(5 answers)ImportError: cannot import name _ColumnEntity Ubuntu20.10 [duplicate](1 answer)C…

Python string formatting with percentage (TypeError: not enough arguments for format string)

The following code fails to run.It goes through a CSV file and retrieves the values and formats them in a array of tuples (a insert query) to be used later. Problem is the csv last column is sometimes …

Circles touching edges

I am struggling with a program to tell whether a created disk touches the edge of a predefined box. The parser keeps saying things such asNameError: global name disksdescription is not defined Warning…

How to split data from a merged cell into other cells in its same row of a Python data frame?

I have a sample of a data frame which looks like this: +---+--------------------------------------------------------------------------------------+---------------+--------------------------------------…

Collect data in chunks from stdin: Python

I have the following Python code where I collect data from standard input into a list and run syntaxnet on it. The data is in the form of json objects from which I will extract the text field and feed …

Getting and calculating stuff through tkinter widets

I was wondering how to calculate stuff using tkinter buttons. Im making a simple program to calculate seconds to hours:minutes:seconds. The user inputs an integer using the entry widget on the seconds …