我试图写打开一个文本文件,并用*代替所有的四个字母的单词的程序。 我一直在瞎搞这个节目多小时。 我似乎无法取得任何进展。 我希望有人能帮助我这一个。 这里是我到目前为止所。 帮助是非常感谢!
def censor():
filename = input("Enter name of file: ")
file = open(filename, 'r')
file1 = open(filename, 'w')
for element in file:
words = element.split()
if len(words) == 4:
file1 = element.replace(words, "xxxx")
alist.append(bob)
print (file)
file.close()
这里修订verison,我不知道这是不是好多了
def censor():
filename = input("Enter name of file: ")
file = open(filename, 'r')
file1 = open(filename, 'w')
i = 0
for element in file:
words = element.split()
for i in range(len(words)):
if len(words[i]) == 4:
file1 = element.replace(i, "xxxx")
i = i+1
file.close()
for element in file:
words = element.split()
for word in words:
if len(word) == 4:
etc etc
这是因为:
说,在你的文件的第一行是“你好,我的名字是约翰”然后循环的第一次迭代: element = 'hello, my name is john'
和words = ['hello,','my','name','is','john']
你需要检查什么是每个字里面从而for word in words
此外,它可能是值得一提的是,在当前的方法,你不支付任何注意标点符号。 请注意,在第一个字words
上面...
为了摆脱标点符号的,而说:
import string
blah blah blah ...
for word in words:
cleaned_word = word.strip(string.punctuation)
if len(cleaned_word) == 4:
etc etc
这里是一个暗示: len(words)
返回字的当前行数,而不是任何特定的字的长度。 您需要添加代码,想看看您的线路上的每一个字说,并决定是否需要更换。
此外,如果该文件是比文字的简单列表更复杂(例如,如果它包含了需要保留的标点字符),它可能会使用正则表达式来完成这项工作是值得的。
它可以是这样的:
def censor():
filename = input("Enter name of file: ")
with open(filename, 'r') as f:
lines = f.readlines()
newLines = []
for line in lines:
words = line.split()
for i, word in enumerate(words):
if len(word) == 4:
words[i] == '**'
newLines.append(' '.join(words))
with open(filename, 'w') as f:
for line in newLines:
f.write(line + '\n')
您需要更换字母w
在w+
第4行: file1 = open(filename, 'w')
富人是file1 = open(filename, 'w+')
我在这个项目上工作压力太大所以这是我的代码)
import os
import time
localtime = time.asctime( time.localtime(time.time()) )
a = input("What is your first name? ").title()
b = input("And your last name? ").title()
c = input("What is your e-mail adderess? ")
d = input("And your phone number? ")
f = input("When were you born? (02-01-1900 is 1 February 1900)")
print(a)
print(b)
print(c)
print(d)
print(f)
e = input("Is this correct? (Y\N) ")
g = (a+"-"+b)
if e == "Y" or "y":
new_path = '/users/Pivo/registreren/%s.txt' % g
new_days = open(new_path,'w+')
new_days.write(localtime)
new_days.write('\n')
new_days.write(a)
new_days.write(" ")
new_days.write(b)
new_days.write('\n')
new_days.write(c)
new_days.write('\n')
new_days.write(d)
new_days.write('\n')
new_days.write(f)
new_days.write('\n')
new_days.write('\n')
new_days.write('\n')
new_days.write('\n')
new_days.close()
print("Okay, done!")
if e == "N" or "n":
os.startfile("C:/Users/Pivo/registreren/registreren.py")
def censor(filename):
"""Takes a file and writes it into file censored.txt with every 4-letterword replaced by xxxx"""
infile = open(filename)
content = infile.read()
infile.close()
outfile = open('censored.txt', 'w')
table = content.maketrans('.,;:!?', ' ')
noPunc = content.translate(table) #replace all punctuation marks with blanks, so they won't tie two words together
wordList = noPunc.split(' ')
for word in wordList:
if '\n' in word:
count = word.count('\n')
wordLen = len(word)-count
else:
wordLen = len(word)
if wordLen == 4:
censoredWord = word.replace(word, 'xxxx ')
outfile.write(censoredWord)
else:
outfile.write(word + ' ')
outfile.close()