【发布时间】:2021-07-27 13:52:07
【问题描述】:
我有一个函数:
def get_translations(string):
"""stuff"""
list1 = []
empty = []
lines = string.splitlines()
order = 1 if string.split(',')[0] == "english" else -1
if 'english,māori' in string:
for seperate_words in lines:
list1.append(tuple(seperate_words.split(",")[::order]))
return list1[1:]
if 'māori,english' in string:
for seperate_words in lines:
list1.append(tuple(seperate_words.split(",")[::order]))
return list1[1:]
else:
print("Header language not recognized!")
return empty
我正在尝试在新函数 get_translations_from_file 中调用此函数,以便执行第一个函数的意图。这是因为我试图打开一个文件并将该文件用作第一个函数的字符串。这是我尝试调用的第一个函数,但没有成功:
def get_translations_from_file(filename):
"""stuff"""
file = open(filename)
string = file.read()
get_translations(string)
测试是:
filename = 'places.txt'
for terms in get_translations_from_file(filename):
print(terms)
文件内容为:
english,māori
New Zealand,Aotearoa
North Island,Te Ika-a-Māui
South Island,Te Waipounamu
Wellington,Te Whanganui a Tara
Christchurch,Ōtautahi
Hamilton,Kirikiriroa
Auckland,Tāmaki Makaurau
【问题讨论】:
标签: python python-3.x function