这是我的主要字符串
"action","employee_id","name"
"absent","pritesh",2010/09/15 00:00:00
所以在名称coolumn之后它会转到新行但是在这里我追加到列表中添加一个新的行字符并使其像这样:
data_list ***** ['"action","employee_id","name"\n"absent","pritesh",2010/09/15 00:00:00\n']
这里它附加了新的线条字符缺席,但实际上它是一条新线条但是它附加了我想让它像
data_list ***** ['"action","employee_id","name","absent","pritesh",2010/09/15 00:00:00']
答案 0 :(得分:9)
Davide的答案可以更简单地写成:
data_list = [word.strip() for word in data_list]
但我不确定这是你想要的。请在python中写一些示例。
答案 1 :(得分:6)
replaces = inString.replace("\n", "");
答案 2 :(得分:3)
首先,您可以使用strip()
摆脱'\n'
:
>>> data = line.strip().split(',')
其次,您可能希望使用csv模块来执行此操作:
>>> import csv
>>> f = open("test")
>>> r = csv.reader(f)
>>> print(r.next())
['action', 'employee_id', 'name']
答案 3 :(得分:1)
def f(word):
return word.strip()
data_list = map(f, data_list)
答案 4 :(得分:1)
我会这样做:
in_string.replace('\n', ',', 1).split(',')