Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文件夹中每个文件中的句点和逗号替换为空格,然后打印文件_Python - Fatal编程技术网

Python 将文件夹中每个文件中的句点和逗号替换为空格,然后打印文件

Python 将文件夹中每个文件中的句点和逗号替换为空格,然后打印文件,python,Python,我有一个文件夹,里面有一堆文件,每个文件都包含一个文本字符串、句点和逗号。我想用空格替换句点和逗号,然后打印所有文件 但是在用空格替换逗号和点并打印文件后,图像中的东西被打印,我不想打印点和逗号,我想在替换逗号和点后打印文件 您的问题不是replace,而是对于拆分为字符而不是单词的文本中的单词 此代码 stopwords = ['and', 'or'] text = "Hello World. Bye." tokens_without_sw = [word for wo

我有一个文件夹,里面有一堆文件,每个文件都包含一个文本字符串、句点和逗号。我想用空格替换句点和逗号,然后打印所有文件

但是在用空格替换逗号和点并打印文件后,图像中的东西被打印,我不想打印点和逗号,我想在替换逗号和点后打印文件


您的问题不是
replace
,而是
对于拆分为字符而不是单词的文本中的单词

此代码

stopwords = ['and', 'or']

text = "Hello World. Bye."
tokens_without_sw = [word for word in text if word not in stopwords]
print(tokens_without_sw)
给予

但是你期待着
[“你好”,“世界”,“再见]

您应该使用
text.split(“”
)来获取单词-但首先您应该替换
逗号和
点,因为您可能有没有空格的文本,如
“World.Bye”
,然后它会将其视为单个单词。此外,如果在删除
停止字列表之前未替换
逗号和
,则可能会保留带有
逗号的停止字,如
或…

stopwords = ['and', 'or']

text = "Hello World. Bye."

text = text.replace('.', ' ').replace(',', ' ')

text = text.replace('  ', ' ')  # convert double space into single space

text = text.strip()  # remove space at the end

tokens_without_sw = [word for word in text.split(' ') if word not in stopwords]

print(tokens_without_sw)
结果:

['Hello', 'World', 'Bye']

使用
或…
regex
删除两个以上空格的示例

import re

stopwords = ['and', 'or']

text = "Hello World. or... Bye."

text = text.replace('.', ' ').replace(',', ' ')
#text = re.sub('\.|,', ' ', text)

#text = text.replace('  ', ' ')  # convert double space into single space
text = re.sub('\s+', ' ', text)  # convert many spaces into single space

text = text.strip()  # remove space at the end

tokens_without_sw = [word for word in text.split(' ') if word not in stopwords]

print(tokens_without_sw)

打印
tokens\u而不打印
,但应打印
filtered\u语句
如果
text
是字符串,则
for word in text
将其拆分为单个字符,而不是单词。您可以使用
text.split(“”)
拆分为单词。但是首先你应该替换逗号和点。代码[在“你好,世界”中逐字逐句]给出['H','e','l','l','o','W','o','r','l','d'],而不是[“你好”,“世界”]
['Hello', 'World', 'Bye']
import re

stopwords = ['and', 'or']

text = "Hello World. or... Bye."

text = text.replace('.', ' ').replace(',', ' ')
#text = re.sub('\.|,', ' ', text)

#text = text.replace('  ', ' ')  # convert double space into single space
text = re.sub('\s+', ' ', text)  # convert many spaces into single space

text = text.strip()  # remove space at the end

tokens_without_sw = [word for word in text.split(' ') if word not in stopwords]

print(tokens_without_sw)