我正在编写一个猪拉丁转换器,向用户询问文件名并打开它将所有单词转换为猪拉丁语并将所有咒语从一个字母更改为“*”
VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
f = open('textfile', 'w')
line = f
words = line.split()
count = 0
for word in words:
for vowel in VOWELS:
if vowel in word:
# Find the first vowel that comes up
i = word.find(vowel)
#Found the first one, stop there!
break
else:
# Set the error value again
i = -1
def find_vowel(word):
# This is from 0 to the length of the word
for i in range(len(word)):
if word[i] in VOWELS:
return i
# Return an error value if there are not any (which solves another problem too!)
return -1
for vowel in file:
if curlet.strip()==vowels:
print('*')
else:
print (curlet,end='')
现在我收到了这个错误:
"Traceback (most recent call last):
File "D:/Python/Projects/piglatin1.py", line 12, in <module>
words = line.split()
AttributeError: '_io.TextIOWrapper' object has no attribute 'split'"
答案 0 :(得分:1)
line
是f
。 f
是您的文件。您无法拆分文件,只能拆分字符串。你的意思是line = f.read()
? line
将是一个误称,因为它将是文件中的所有数据,但您的代码中没有任何内容可以循环遍历这些行并一次处理它们。