Python字符串
每个数字都应该 替换为拼写出来的名称(零、一、二、三、四、五、六、七、八、九),但我的新文件中一直有这样的内容:Python字符串,python,string,mirror,Python,String,Mirror,每个数字都应该 替换为拼写出来的名称(零、一、二、三、四、五、六、七、八、九),但我的新文件中一直有这样的内容: zero0000000001one111111112222222222333three3333334444four4444455555five5555666666six666 这是我的节目: def numbers(fileName): #open the inputed file ,prompt for the file inFile= open(fileNa
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()
这是你的问题
for word in wordList:
if word == '0':
outFile.write("zero")
else:
outFile.write(word)
对于每个不是“0”的单词,输出“else”部分中的单词。因此,例如,由于不是0
,每个1都将打印出1
,即使它以后打印出1
我将此问题称为“早期默认”问题,即在第一次检查失败时执行默认操作。为了避免“提前违约”的问题,尽可能长时间推迟违约行动。在这种情况下,你想要一个大的如果。。。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)
然而,一个更具python风格的公式是使用一个列表:
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)
这样可以避免编写huuuge if/elif/else链,并且更易于维护(例如,您可以一次对所有数字词执行操作,例如,使其大写,或从文件中加载,或…您的if/else块非常混乱。您应该去掉所有的
else
语句,并在第一个if
之后使用elif
,如下所示:
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)
将单独的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")
如果您希望为所有数字拼写名称,那么为什么要使用
if word == "7":
outFile.write(word)
7,8,9?我认为这是错误的
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,请使用数字名称.items(),而不是数字名称.iteritems()
digit_name = {
'1': 'one',
'2': 'two',
'3': 'three',
...
}
for word in wordList:
outFile.write(digit_name.get(word, word))
new_word_list = [digit_name.get(word, word) for word in wordlist]
我将从一个用于将数字映射到其名称的字典开始,然后定义一个函数来获取数字的字符串表示形式,并返回使用此映射展开的字符串 为了使它更灵活一点,我需要一个标志(宽容)来过滤输出中的任何非数字,或者保留它们,另外一个标志允许调用者提供自己的自定义分隔符
#!/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
我使用列表理解作为一个单行程序,也作为一个更可读和更明确的循环(我更喜欢这样)。无需使用字典,因为名称列表可以通过int(word)访问
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()