我有一个文本文件,我想删除标点符号并将其另存为新文件,但不删除任何想法,为什么?
代码:
def punctuation(string):
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for x in string.lower():
if x in punctuations:
string = string.replace(x, "")
# Print string without punctuation
print(string)
file = open('ir500.txt', 'r+')
file_no_punc = (file.read())
punctuation(l)
with open('ir500_no_punc.txt', 'w') as file:
file.write(file_no_punc)
为什么删除标点符号?
def punctuation(string):
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for x in string.lower():
if x in punctuations:
string = string.replace(x, "")
# Print string without punctuation
print(string)
file = open('ir500.txt', 'r+')
file_no_punc = (file.read())
file_no_punc = punctuation(file_no_punc)
with open('ir500_no_punc.txt', 'w') as file:
file.write(file_no_punc)
说明:
我仅将punctuation(l)
更改为file_no_punc = punctuation(file_no_punc)
1)l
中的punctuation(l)
是什么?2)您正在呼叫punctuation()
-它可以正常工作-但不要使用其返回值
[请注意,我仅做了最小的更改以使其起作用。您可能需要将其发布到我们的code review网站,以查看如何进行改进。