我试图以Python的方式读取数据,就像这样
states = """
Alabama
Alberta
Alaska
Arizona
Arkansas
Bob
Tom
Ted
William
"""
states_list = [w.strip().lower() for w in states.splitlines() if w]
现在,如果我尝试从文件中读取类似的数据,那么它无效。我是这样做的
file1 = open('dictionary_file.txt','r')
data = file1.read()
file1.close()
然后迭代数据中的项目
这是整个代码,相关部分在最后。
def _get_child_branches(tree):
"""
This method return all the branches of the tree
"""
return tree[1:]
def _get_child_branch(tree, c):
"""
This method returns the specific branch of the tree given the character
"""
for branch in _get_child_branches(tree):
if branch[0] == c:
return branch
return None
def _retrive_branch(k, tree):
"""
This method is used for getting the branch for a given word
"""
if not k:
return None
for 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]) is tuple
def _get_bucket_key(bucket):
if not _is_tree_bucket(bucket):
return None
return bucket[1][0]
def has_key(k, tree):
"""
To check if the tree containes the keyword
"""
return _retrive_branch(k, tree) is 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 a (key, value) pair into tree
"""
if not key or has_key(key, tree):
return
for 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 with 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() for w in states.splitlines() if w]
print(states_list)
print(states)
for state in data:
insert_key(state, True, tree)
print start_with_prefix("a", tree)
答案 0 :(得分:2)
只需用file object替换分割线并迭代:
with open('dictionary_file.txt','r') as f:
lines =[w.strip().lower() for w in f if w]
除非你真的需要一次数据,文件对象是可迭代的并且在你迭代时会给你每一行,所以永远不需要将整个文件读入内存。
在旁注中,您需要在分界线代码中if w
,因为您在行之前和之后添加换行符,在引号处开始您的字符串并在引号处结束它并且您不会得到任何空字符串:
states = """Alabama
Alberta
Alaska
Arizona
Arkansas
Bob
Tom
Ted
William"""
答案 1 :(得分:1)
with open('dictionary_file.txt', 'r') as f:
lines = f.readlines()
states_list = [w.strip().lower() for w in lines if w]
修改:我意识到这里不需要使用readlines()
。只需使用@ Padraic的答案。
答案 2 :(得分:-1)
添加.split('\n')
将解决此问题。
file1 = open('dictionary_file.txt','r')
data = file1.read().split('\n')
file1.close()
for i in data:
print i