請幫幫我!蟒蛇拉丁轉換器
我正在將多行文本文件轉換爲拉丁文。
例如:豬的拉丁語翻譯:這是一個例子。應該是:Histay siay naay xampleeay。
我需要任何標點符號留在應該是的位置(大多數情況下句尾) 我還需要任何以大寫字母開頭的單詞,以拉丁語版的大寫字母開頭,其餘字母小寫。
這是我的代碼:
def main():
fileName= input('Please enter the file name: ')
validate_file(fileName)
newWords= convert_file(fileName)
print(newWords)
def validate_file(fileName):
try:
inputFile= open(fileName, 'r')
inputFile.close()
except IOError:
print('File not found.')
def convert_file(fileName):
inputFile= open(fileName, 'r')
line_string= [line.split() for line in inputFile]
for line in line_string:
for word in line:
endString= str(word[1:])
them=endString, str(word[0:1]), 'ay'
newWords="".join(them)
return newWords
我的文本文件是:
This is an example.
My name is Kara!
且程序返回:
Please enter the file name: piglatin tester.py
hisTay
siay
naay
xample.eay
yMay
amenay
siay
ara!Kay
None
如何讓他們在打印出來他們在線?而且我該如何處理標點問題和大寫?
謝謝!然而,我得到這個錯誤:文件「/Users/tinydancer9454/Documents/python/pigLatinFile.py」,第17行,在主要 strip_punc(行) UnboundLocalError:本地變量'行'之前引用 – tinydancer9454 2013-03-14 03:21:10
也是什麼new_lines引用? – tinydancer9454 2013-03-14 03:26:11
new_lines指從英文轉換成PigLatin的行。 – ChrisGuest 2013-03-14 03:30:05