2

每个数字都应替换为拼写出来的名称(零、一、二、三、四、五、六、七、八、九),但我一直在我的新文件中得到这个:

zero0000000001one111111112222222222333three3333334444four4444455555five5555666666six666 

这是我的程序:

def numbers(fileName):
    #open the inputed file ,prompt for the file 
    inFile= open(fileName,'r') #this will open the function for writing and reading 
    outFile=open('converted.txt', 'w')
    for line in inFile:
        wordList=line.split()
        for word in wordList:
            if  word == '0':
                outFile.write("zero")
            else:
                outFile.write(word) 

            if word =="1":
                outFile.write("one")
            #else:
                #outFile.write(word)

            if word in wordList == "2":
                outFile.write("two")
            #else:
                #outFile.write(word)

            if word == ("3"):
                outFile.write("three")
            #else:
                #outFile.write(word)

            if word == ("4"):
                outFile.write("four")
            #else:
                #outFile.write(word)

            if word == ("5"):
                outFile.write("five")
            #else:
                #outFile.write(word)

            if word == ("6"):
                outFile.write("six")
            #else:
                #outFile.write(word)

            if word == ("7"):
                outFile.write(word)
            #else:
                #outFile.write(word)

            if word == ("8"):
                outFile,write(word)
            #else:
                #outFile.write(word)

            if word == ("9"):
                outFile.write(word)
            #else:
                #outFile.write(word)
    outFile.write(" ")
    outFile.write("\n")
    outFile.close()
    inFile.close()
4

8 回答 8

2

这是你的问题

    for word in wordList:
        if  word == '0':
            outFile.write("zero")
        else:
            outFile.write(word) 

对于每个不是 '0' 的单词,你会输出 'else' 部分中的单词。因此,例如每个 1 都会1因为不存在而打印出来0,即使它后来打印出来one

我将此问题称为“早期默认”问题,即您在第一次检查失败时执行默认操作。为了不遭受“早期默认”问题,请尽可能延迟执行默认操作。在这种情况下,您需要一个大的 if... else if 链,其中包含特殊单词(== "0" 到 =="9")的所有可能结果,然后 else if 链的最后一个 else 将是写字的默认动作。

就像是

        if word == "0":
            outFile.write("zero")
        elif word == "1":
            outFile.write("one")
        elif word == "2":
            outFile.write("two")
...
        else:
            outFile.write(word)

然而,一个更 Pythonic 的公式是使用一个列表:

numberWords = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

然后在 for 循环中执行此操作:

try:
    outFile.write(numberWords[int(word)]) # attempt to convert word to an int, then look in the list for its word
except ValueError: # if word was not a string version of an int
    outFile.write(word)

这使您无需编写 huuuuuuuge if/elif/else 链,并且也更易于维护(例如,您可以一次对所有 numberWords 进行操作,例如将它们设为大写,或从文件中加载它们,或者。 ..)

于 2013-04-19T02:26:58.710 回答
2
digit_names = {'1': 'one',
               '2': 'two',
               ...
               '9': 'ten'}

mystring = open('in.txt', 'r').read()
for d, n in digit_names.iteritems():
    mystring = mystring.replace(d, n)

open('converted.txt', 'w').write(mystring)

这就是你需要的一切。对于 python3,使用 digit_names.items(),而不是 digit_names.iteritems()。

于 2013-04-19T02:33:17.460 回答
0

无需使用字典,因为可以通过 int(word) 访问名称列表

def numbers(fileName):
    #open the inputed file ,prompt for the file 
    inFile= open(fileName,'r') #this will open the function for writing and reading 
    outFile=open('converted.txt', 'w')
    for line in inFile:
        wordList=line.split()
        names = ['zero', 'one', 'two', 'three', 'four',
                 'five', 'six', 'seven', 'eight', 'nine']
        [outFile.write(names[int(word)]) for word in wordList]
    outFile.write(" ")
    outFile.write("\n")
    outFile.close()
    inFile.close()
于 2013-04-19T03:28:35.533 回答
0

您的 if/else 块非常混乱。你应该去掉所有的else语句,并elif在第一个之后使用if,如下所示:

    for word in wordList:
        if  word == '0':
            outFile.write("zero")
        elif word =="1":
            outFile.write("one")
        elif word == "2": # note that what you had here was very bad: if word in wordList == "2":
            outFile.write("two")
        elif word == "3":
            outFile.write("three")
        elif word == "4":
            outFile.write("four")
        elif word == "5":
            outFile.write("five")
        elif word == "6":
            outFile.write("six")
        elif word == "7":
            outFile.write("seven")
        elif word == "8":
            outFile,write("eight")
        elif word == "9":
            outFile.write("nine")
        else:
            # If you want to leave any other character unchanged, then you say:
            outFile.write(word)
于 2013-04-19T02:29:02.523 回答
0

将您的个别 if/else 语句更改为一个 if/elif/else 语句

if word == '1':
     outFile.write("one")
 elif word == '2':
      outFile.write("two")
 elif word == '3':
      outFile.write("three")
 else:
      outFile.write("four")
于 2013-04-19T02:29:06.870 回答
0

如果您希望所有数字都拼出名称,那么您为什么使用

if word == "7":
    outFile.write(word)

7,8,9?我认为那是错误

于 2013-04-19T02:32:58.343 回答
0

我将从用于将数字映射到其名称的字典开始,然后定义一个函数来获取数字的字符串表示形式并返回使用此映射扩展的字符串。

为了让它更灵活一点,我有一个标志(宽容)来过滤输出中的任何非数字,或者保留它们,另一个允许调用者提供他们自己的自定义分隔符。

    #!/usr/bin/python

    digit_names = {
        '0': 'zero',
        '1': 'one',
        '2': 'two',
        '3': 'three',
        '4': 'four',
        '5': 'five',
        '6': 'six',
        '7': 'seven',
        '8': 'eight',
        '9': 'nine'
        }

    def digit2name(num, tolerant=True, separator=''):
        '''Replace a number (string of digits) with an expansion into the
           mapping of each digit to its name.
        '''
        return separator.join([digit_names.get(x,(x,'')[tolerant]) for x in num])

        '''
        results = list()
        num = str(num)
        for digit in num:
            if tolerant:
                default=digit
            else:
                default=''
            results.append(digit_names.get(digit,digit))
        return separator.join(results)
        '''

    if __name__ == '__main__':
        import sys
        for each in sys.argv[1:]:
            print digit2name(each),
            print digit2name(each, False, '.')
            print

我已经使用列表理解作为单行程序以及更易读和更明确的循环(我更喜欢)来完成此操作。

于 2013-04-19T02:52:04.807 回答
-1
  1. 首先构建一个字典来存储数字映射到它的名字

    digit_name = {
             '1': 'one',
             '2': 'two',
             '3': 'three',
             ...
             }
    
  2. 然后在写入文件时格式化

    for word in wordList:
        outFile.write(digit_name.get(word, word))
    

或者将输出存储在列表中,然后写入文件一次。

new_word_list = [digit_name.get(word, word) for word in wordlist]
于 2013-04-19T02:40:42.220 回答