我正在自学Python并给自己随机任务努力,我试图从我从文本文件创建的列表中的每个元素的末尾删除\ n?
我见过: How to remove \n from a list element?
然而,应用这个似乎并不适合我,我不确定为什么?是否有人能够建议如何从每个元素的末尾剥离\ n?
word =[]
finalList =[]
#Problem area
dictionary = open ('FILELOCATION\dictionary.txt', 'r')
for line in dictionary.readlines():
word.append([line])
for i in line.split():
finalList[-1].append(i)
#Looping over the list
n = 0
while n < 25:
print (finalList[n])
n += 1
答案 0 :(得分:0)
final_list = []
with open("accounts.txt","r") as my_file:
first_list = my_file.readlines()
for i in first_list:
final_list.append(i.strip())
根据您的需要进行更改,这会删除所有\ n。