【问题标题】:I'm coding a PigLatin converter to call a file?我正在编写一个 PigLatin 转换器来调用文件?
【发布时间】:2014-02-22 16:32:44
【问题描述】:

我正在编写一个猪拉丁语转换器,它要求用户输入文件名并打开它,将所有单词转换为猪拉丁语并将所有元音从字母更改为“*”

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'"

【问题讨论】:

    标签: python python-3.x python-3.2


    【解决方案1】:

    lineff 是您的文件。您不能拆分文件,只能拆分字符串。你的意思是line = f.read()line 将是用词不当,因为它将是文件中的所有数据,但是您的代码中没有任何内容可以遍历行并一次处理一个。

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 1970-01-01
      • 2012-05-15
      • 2021-11-16
      • 2016-02-17
      • 2021-12-22
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      相关资源
      最近更新 更多