string - Reading data in python? -


i trying read data in python 1 way

states = """         alabama         alberta         alaska         arizona         arkansas         bob         tom         ted         william         """ states_list = [w.strip().lower() w in states.splitlines() if w] 

now if trying read similar data file not working.

file1 = open('dictionary_file.txt','r') data = file1.read() file1.close() 

and iterate on items in data

here entire code, related part @ end.

def _get_child_branches(tree): """ method return branches of tree """ return tree[1:]   def _get_child_branch(tree, c): """ method returns specific branch of tree given character """ branch in _get_child_branches(tree):     if branch[0] == c:         return branch  return none   def _retrive_branch(k, tree): """ method used getting branch given word """ if not k:     return none  c in k:     child_branch = _get_child_branch(tree, c)     if not child_branch:         return none     tree = child_branch  return tree   def _is_tree_bucket(bucket): if len(bucket) != 2:     return false  return type(bucket[1]) tuple   def _get_bucket_key(bucket): if not _is_tree_bucket(bucket):     return none  return bucket[1][0]   def has_key(k, tree): """ check if tree containes keyword """ return _retrive_branch(k, tree) not none   def retree_val(k, tree): key_tuple = _retrive_branch(k, tree) if not key_tuple:     return none  return key_tuple[1]   def insert_key(key, v, tree): """ insert (key, value) pair tree """ if not key or has_key(key, tree):     return  char in key:     branch = _get_child_branch(tree, char)     if not branch:         new_branch = [char]         tree.append(new_branch)         tree = new_branch     else:         tree = branch tree.append((key, v))   def start_with_prefix(prefix, tree): """ find words start prefix """ branch = _retrive_branch(prefix, tree) if not branch:     return []  prefix_list = [] q = branch[1:] while q:     curr_branch = q.pop(0)     if _is_tree_bucket(curr_branch):         prefix_list.append(_get_bucket_key(curr_branch))     else:         q.extend(curr_branch[1:])  return prefix_list       if __name__ == "__main__": tree = [[]] file1 = open('dictionary_file.txt','r') data = file1.read().split('\n') file1.close() states = """         alabama         alberta         alaska         arizona         arkansas         bob         tom         ted         william""" states_list = [w.strip().lower() w in states.splitlines() if w] print(states_list) print(states) state in data:     insert_key(state, true, tree)   print start_with_prefix("a", tree) 

adding .split('\n') resolve it.

file1 = open('dictionary_file.txt','r') data = file1.read().split('\n') file1.close() in data:     print 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -