2024-10-01 22:34:23 发布
网友
我想删除文本文件每行末尾的\n,因为我需要将每行作为单独的列表项放入列表中。在
with open("PythonTestFile.txt", "rt") as openfile: for line in openfile: new_list = [] new_list.append(line) print(new_list)
这就是我得到的
尝试使用string.strip()
string.strip()
with open("PythonTestFile.txt", "rt") as openfile: new_list = [] for line in openfile: new_list.append(line.rstrip('\n')) print(new_list)
line = line.rstrip('\n')
这将删除行末尾的换行符。在
尝试使用
string.strip()
这将删除行末尾的换行符。在
相关问题 更多 >
编程相关推荐