我有一个.txt文件比我用python读的文件。我想替换包含短划线“ - ”的所有单词,并且用空格替换少于4个字符。有谁知道如何做到这一点?
我现在有这个:
with open('text.txt', 'r') as file:
filedata = file.read()
filedata = filedata.replace('bla', '')
with open('new text.txt', 'w') as file:
file.write(filedata)
答案 0 :(得分:0)
以下程序应该完成这项工作
a = open("Location of the file",'r')
b= a.read()
a.close()
c = b.split()
str = ""
for i in c:
if (((i.find('-')) != -1) and len(i)<4 ):
pass
else:
str = str+i+" "
print(str)
a = open("Location of the file",'w')
a.write(str)
a.close()