用python读取数据?

用python读取数据?,python,string,file,io,Python,String,File,Io,我正在尝试用Python读取数据,我这样做的一种方式是 states = """ Alabama Alberta Alaska Arizona Arkansas Bob Tom Ted William """ states_list = [w.strip().lower() for w in states.splitlines() if

我正在尝试用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()
现在,如果我试图从一个文件中读取类似的数据,它将不起作用。我是这样做的

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)
请读一读


编辑:我意识到这里不需要使用
readlines()
。只需使用@Padraic的答案。

只需将拆分线替换为a,然后迭代:

with open('dictionary_file.txt','r') as f:
   lines  =[w.strip().lower() for w in f if w]
永远不需要将整个文件读入内存,除非您实际上需要立即读取数据,否则file对象是可编辑的,并且在迭代时会给出每一行

另一方面,当您在行之前和之后添加换行符时,如果拆分行代码中有w,则需要将字符串从引号开始,并在引号结束,这样您就不会得到任何空字符串:

states = """Alabama
    Alberta
    Alaska
    Arizona
    Arkansas
    Bob
    Tom
    Ted
    William"""
添加
.split('\n')
将解决此问题

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

你的问题是?三重引号不应该只用于docstring吗?出于某种原因它不起作用。。。我的txt文件只有一个单词列表,当我的程序中有这些单词时,它可以正常工作,但如果我尝试使用它提供的error@user2939685它应该会起作用。您得到的错误是什么?然而,我认为你应该同意帕德雷克的回答,这更为优雅。当我打印这两张照片时,它们是这样打印的,这意味着有一些问题……[“阿拉巴马州”、“阿尔伯塔州”、“阿拉斯加”、“亚利桑那州”、“阿肯色州”、“鲍勃”、“汤姆”、“泰德”、“威廉”]阿拉巴马州阿尔伯塔州阿拉斯加州亚利桑那州阿肯色州鲍勃·汤姆·特德·威利米没有明白问题出在哪里?你能用预期的输入/输出更新原始问题吗?如果我在插入项目的最后一个for循环中用state_list替换数据,代码就会起作用。我想最后一段是给我的:P@Sait,我总是这么说,不是针对个人的!许多人一开始总是调用readlines或read,然后将read拆分成一列行,或者迭代readlines,这样做他们可以只迭代file对象。