我有一个python列表,其中如下所示:
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']
如何从每个元素的末尾删除\ n以及为什么条带不起作用?
我尝试了line.strip('\n')
和line.replace('\n','')
,但它没有影响任何内容。
制作列表的文本文件如下所示:
Java Basic
Core Java
C++
Python
...
将文本文件放入列表的代码是:
with open("myfile.txt") as f:
for line in f:
st_line = line.strip('\n')
d.append(st_line)
答案 0 :(得分:1)
尝试这样的事情:
with open("myfile.txt") as f:
d = map(str.rstrip, f.readlines())
print d
对于您上面的输入,这将输出:
['Java Basic', 'Core Java', 'C++', 'Python']