在python中如何从字符串或列表中删除这个\n

时间:2021-09-23 21:41:44

This question already has an answer here:

这个问题已经有了答案:

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

因此,在名称coolumn之后,它会转到新行,但这里我添加了一个新的行字符,并以这种方式添加。

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

data_list * * * * *(“行动”,“employee_id”,“名字”\ n“缺席”,“pritesh 2010/09/15就是\ 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

这里它附加了新的行字符,但实际上它是一个新的行strarting,但它的appended我想要它的样子。

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

data_list * * * * *(“行动”,“employee_id”,“名字”,“缺席”,“pritesh 2010/09/15就是”)

5 个解决方案

#1


8  

Davide's answer can be written even simpler as:

Davide的回答可以写得更简单:

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.

但我不确定这是否是你想要的。请用python写一些样例。

#2


5  

replaces = inString.replace("\n", "");

#3


3  

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

首先,您可以使用strip()来除去“\n”:

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

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

其次,您可能想要使用csv模块:

>>> import csv
>>> f = open("test")
>>> r = csv.reader(f)
>>> print(r.next())
['action', 'employee_id', 'name']

#4


1  

def f(word):
    return word.strip()
data_list = map(f, data_list)

#5


1  

I'd do like that:

我会做像这样:

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

#1


8  

Davide's answer can be written even simpler as:

Davide的回答可以写得更简单:

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.

但我不确定这是否是你想要的。请用python写一些样例。

#2


5  

replaces = inString.replace("\n", "");

#3


3  

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

首先,您可以使用strip()来除去“\n”:

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

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

其次,您可能想要使用csv模块:

>>> import csv
>>> f = open("test")
>>> r = csv.reader(f)
>>> print(r.next())
['action', 'employee_id', 'name']

#4


1  

def f(word):
    return word.strip()
data_list = map(f, data_list)

#5


1  

I'd do like that:

我会做像这样:

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