Suppose I have a data set like this
Language: English
Place: Seattle
Segments: b,p,m,d,t
Language: Mandarin
Place: HK
Segments: n,i,o,h
Language: Cantonese
Place:HK
Segments:l,e,h,o
and it continues to have a pattern like this.
How would I make a definition function that would check what language(s) a specific city has.
What I have so far is:(though it is not right) language=list()
def lang_from(location):
file=open("data.txt")
lang = file.readline().replace("/n", "").replace("Language:", "")
city = file.readline().replace("/n", "").replace("Place:", "")
seg = file.readline().replace("/n", "").replace("Segments:", "")
for place in file:
if location in place:
languages.append(language.lang)
else:
break
I want my input to be :
print(lang_from("HK"))
and the output to be
Cantonese, Mandarin
maybe regular expressions would be the easiest way (although it might be hard to catch edge cases:
You can tinker with the regex here.