如何使用python从文本文件的每一行删除一个字符?

2024-10-01 22:34:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想删除文本文件每行末尾的\n,因为我需要将每行作为单独的列表项放入列表中。在

with open("PythonTestFile.txt", "rt") as openfile:

    for line in openfile:
        new_list = []

        new_list.append(line)

        print(new_list)

这就是我得到的

^{pr2}$

Tags: intxt列表newforaswithline
2条回答

尝试使用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')

这将删除行末尾的换行符。在

相关问题 更多 >

    热门问题