0

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
Robbie
  • 61
  • 5

2 Answers2

0

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.

quapka
  • 2,799
  • 4
  • 21
  • 35
0

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]
Rápli András
  • 3,869
  • 1
  • 35
  • 55
  • This is not the correct answer, as far as I can tell. But only a minor edit would solve it. Check the whitespaces in the question. Your code would not catch difference between `Place:HK` and `Place: HK`. Using `str.strip()` on `value` in the `for` loop should solve the issue. – quapka Dec 14 '16 at 14:25
  • 1
    Yes, I overlooked that, but it's just a space bro :) – Rápli András Dec 14 '16 at 15:06
  • Yeah, I did not want to sound so pedantic, sorry for that. But _it's just a space_ can lead to soo many unwanted problems, which could be very hard to find.. So I sort of trained myself to save myself from those troubles in advance.. – quapka Dec 14 '16 at 15:20