读取文本文件时删除标点符号[python]

2024-09-28 17:27:45 发布

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

我正在编写一个python程序,它将文本文件的内容读入数组/列表中,但是在从文本文件中删除标点符号时遇到了问题。以下是我尝试过的:

def read_file(self,filename):
    name_file = filename
    filename = open(name_file, 'r')
    file = filename
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    no_punct = ""
    lst = []
    for word in file:
        word = word.strip('\n')
        for char in punctuations:
            word = word.strip(char)
        lst.append(word)


    filename.close()

在去掉字符的部分,我注意到word文件中内容的顺序也发生了变化,一些标点符号没有完全删除。在

如果我使用'replace'方法,它工作得很好,但是我正在寻找一个不使用replace内置函数的方法。在


Tags: 方法namein内容forfilenamereplaceword
1条回答
网友
1楼 · 发布于 2024-09-28 17:27:45

我注意到的一些事情只会导致一些标点被删除。行for word in file:实际上应该是for line in file:。Python通过行而不是单词迭代文件。strip函数只从开头和结尾删除项。您可以使用replace函数从中间删除字符。程序当前的编写方式只会删除文档中每行开头和结尾的标点符号。在

我删除所有标点符号的方式是这样的。在

from pathlib import Path
import string

filepath = Path(filename)
text = filepath.read_text()
text = text.replace(string.punctuation, "")
filepath.write_text(text )

但是你说replace函数和ebook功能弄乱了。你能再详细解释一下吗。我看不出替换每个单词中的标点符号与同时替换整个文件中的标点符号有什么不同?在

相关问题 更多 >