How to make a definition function to find particular symbols

46 views Asked by At

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
2

There are 2 answers

0
quapka On

maybe regular expressions would be the easiest way (although it might be hard to catch edge cases:

import re

def lang_from(location):

    pattern = 'Language:\s*(\w+)\s*Place:\s*{}'.format(location)
    with open('data.txt', 'r') as f:
        print(re.findall(pattern, f.read()))

if __name__ == '__main__':

    lang_from('HK')
# prints ['Mandarin', 'Cantonese']

You can tinker with the regex here.

3
Rápli András On

Split the raw data at two line feeds, you get

 Language: English

 Place: Seattle

 Segments: b,p,m,d,t

Trim empty lines for

 Language: English
 Place: Seattle
 Segments: b,p,m,d,t

result_list = your_string.split('\n').rstrip()

 ['Language: English','Place: Seattle','Segments: b,p,m,d,t']

Make a loop like this:

dict = {}
for r in result_list:
    item = r.split(':')
    key = item[0] # Language
    value = item[1].strip() # English
    dict[key] = value

At this point you have a nice structured dictionary, which you can filter through easily.

{
 'Language': 'English',
 'Place': 'Seattle',
 'Segments': 'b,p,m,d,t'
}

How would I make a definition function that would check what language(s) a specific city has.

def get_language_for(city):
    return [ v['Language'] for k, v in dict.items() if v['Place'] == city ][0]