Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in python how to remove this \n from string or list [duplicate]

Tags:

python

string

this is my main string

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

so after name coolumn its goes to new line but here i append to list a new line character is added and make it like this way

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

here its append the new line character with absent but actually its a new line strarting but its appended i want to make it like

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

like image 646
pritesh modi Avatar asked May 25 '10 09:05

pritesh modi


3 Answers

Davide's answer can be written even simpler as:

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

But I'm not sure it's what you want. Please write some sample in python.

like image 157
zefciu Avatar answered Sep 21 '22 00:09

zefciu


replaces = inString.replace("\n", "");
like image 42
InsertNickHere Avatar answered Sep 18 '22 00:09

InsertNickHere


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

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

Secondly, you may want to use the csv module to do that:

>>> import csv
>>> f = open("test")
>>> r = csv.reader(f)
>>> print(r.next())
['action', 'employee_id', 'name']
like image 26
e-satis Avatar answered Sep 20 '22 00:09

e-satis