Python - strings
每个数字都应该用它的名字代替它(0,1,2,3,4,5,6,7,8,9),但我不断地得到这个,就像在我的新文件:
1 | zero0000000001one111111112222222222333three3333334444four4444455555five5555666666six666 |
这是我的计划:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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(" ") outFile.close() inFile.close() |
号
1 2 3 4 5 6 7 8 9 10 | 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_name.items(),而不是digit_name.iteritems()。
这是你的问题
1 2 3 4 5 | for word in wordList: if word == '0': outFile.write("zero") else: outFile.write(word) |
对于不是"0"的每个单词,您输出"else"部分中的单词。因此,例如,每个1都将打印出
我把这个问题称为"早期违约"问题,即当支票第一次失败时,您将执行默认操作。为了不受"早期违约"问题的影响,请尽可能推迟执行违约操作。在这种情况下,你想要一个大的如果…否则,如果单词的每个可能结果链都是特殊的(=="0"到=="9"),那么else if链的最后一个else将是写入单词的默认操作。
有点像
1 2 3 4 5 6 7 8 9 | if word =="0": outFile.write("zero") elif word =="1": outFile.write("one") elif word =="2": outFile.write("two") ... else: outFile.write(word) |
号
然而,一个更为Python式的配方将使用一个列表:
1 | numberWords = ["zero","one","two","three","four","five","six","seven","eight","nine"] |
然后在for循环中执行此操作:
1 2 3 4 | 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链,也更容易维护(例如,您可以一次对所有数字字执行操作,例如将其设为大写,或从文件中加载它们,或…)
如果你想把名字拼成所有数字,那你为什么要用
1 2 | if word =="7": outFile.write(word) |
。
7、8、9?我想那是个错误
不需要使用字典,因为可以通过int(word)访问名称列表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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(" ") outFile.close() inFile.close() |
号
我将从一个将数字映射到其名称的字典开始,然后定义一个函数来获取数字的字符串表示,并返回用这个映射扩展的字符串。
为了使其更加灵活,我需要一个标志(允许),要么过滤输出中的任何非数字,要么保留它们,另一个标志允许调用者提供自己的自定义分隔符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #!/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, '.') |
号
我使用列表理解作为一个一行程序完成了这项工作,并且作为一个更可读和更明确的循环(我更喜欢这样)。
将您的个人if/else语句更改为一个if/elif/else语句
1 2 3 4 5 6 7 8 | if word == '1': outFile.write("one") elif word == '2': outFile.write("two") elif word == '3': outFile.write("three") else: outFile.write("four") |
。
你的if/else模块非常混乱。您应该去掉所有的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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) |
首先构建一个dict来存储到其名称的数字映射
1 2 3 4 5 6 | digit_name = { '1': 'one', '2': 'two', '3': 'three', ... } |
。
然后在写入文件时格式化它
1 2 | for word in wordList: outFile.write(digit_name.get(word, word)) |
或者将输出存储在一个列表中,然后写入文件一次。
1 | new_word_list = [digit_name.get(word, word) for word in wordlist] |
。