我想我到目前为止已经读到了字典,但后来我很丢失了。我试图做一个if循环条件(如果密钥在我要翻译的文本中,它应该被值替换)但是我不能从那里继续。
所以这就是我到目前为止所得到的:
dictionary = {}
with open("dict.txt") as dict:
for line in dict:
wordPair = line.split()
dictionary[wordPair[0]] = wordPair[1]
def get_translations(dict):
dict = get_translations("dict.txt")
return get_translations(dict)
def translate(dict, text1, trnsl):
with open("text1.txt") as text1, open("trnsl.txt", "w") as trnsl:
for wordPair in dict:
if wordPair == True: #that's where I can't figure it out.
旁注:如果任何单词无法从给定的字典中翻译(因为它不是字典的一部分),则应由这些“<>”表示。我猜一个简单的说法:打印字+“<>”会做?
感谢您的任何建议!
答案 0 :(得分:1)
您必须指定是否有标点符号或其他内容。
我会假设空格和单词。
def translate(dict, text1, trnsl):
with open("text1.txt") as text1, open("trnsl.txt", "w") as trnsl:
for line in text1: # parse each line
words = line.split() # break line into words, do more stuff here if punctuation
for word in words:
if word in dict: # check if translatable
trnsl.write(dict[word]) # get translation
else:
trnsl.write(word)
trnsl.write(' ') # re-add spaces between words
trnsl.write('\n') # re-add endlines between lines
答案 1 :(得分:0)
如果我理解你的问题,你想要像这样循环字典,以获得每个项目的关键和价值:
var videoArea = document.querySelector{"video"};