In python, how to remove this \ n from a string or list

this is my main line

"action","employee_id","name" "absent","pritesh",2010/09/15 00:00:00 

so after the name coolumn it goes to a new line, but here I add a new line character to the list and does it like this

data_list ***** ['"action","employee_id","name"\n"absent","pritesh",2010/09/15 00:00:00\n']

here a new line symbol is added with the missing one, but in fact its new line is strarting, but its added I want to do it like

data_list ***** ['"action","employee_id","name","absent","pritesh",2010/09/15 00:00:00']

+4
source share
5 answers

David's answer can be written even simpler:

 data_list = [word.strip() for word in data_list] 

But I'm not sure what you want. Please write some examples in python.

+8
source
 replaces = inString.replace("\n", ""); 
+5
source

First, you can use strip() to get rid of '\n' :

 >>> data = line.strip().split(',') 

Secondly, you can use the csv module for this:

 >>> import csv >>> f = open("test") >>> r = csv.reader(f) >>> print(r.next()) ['action', 'employee_id', 'name'] 
+3
source
 def f(word): return word.strip() data_list = map(f, data_list) 
+1
source

I would do this:

 in_string.replace('\n', ',', 1).split(',') 
+1
source

Source: https://habr.com/ru/post/1310834/


All Articles