简体   繁体   English

如何根据从文本文件导入的数据创建 Python 字典?

[英]How to create a Python dictionary from data imported from a text file?

I have the following text in a file named my_text.txt :我在名为my_text.txt的文件中有以下文本:

David: 2
Barbara: 97.2
David: nothing
William:

Lynn: 725
Nancy   : 87
     David:       54
Lewis: 18.30
Sue:   3193.74
James: 41.73

David: 974.1

I would like to be able to read this file and create a dictionary.我希望能够读取这个文件并创建一个字典。

Here is my code:这是我的代码:

def make_dictionary(file_name):

    d = {}

    with open(file_name, 'r') as file:
        for line in file:
            (key, val) = (line.split(':')[0], line.split(':')[1])

    return print(d)

make_dictionary('my_text.txt')

I'm getting an index error:我收到一个索引错误:

IndexError: list index out of range IndexError:列表索引超出范围

Does anyone see where the mistake is?有谁看到错误在哪里?

Thanks!谢谢!

This error arises due to the blank lines.由于空白行而出现此错误。

Your code assumes the split will return two elements for you.您的代码假定split将为您返回两个元素。

You'll want to check for blank lines before trying to use the return value of the split -- and skip those lines.在尝试使用split的返回值之前,您需要检查空行——并跳过这些行。

Also, more robust code would also check that the line contains a colon ( : ) on which to split on.此外,更健壮的代码还会检查该行是否包含要拆分的冒号 ( : )。 If the line contains no colon, the code would fail.如果该行不包含冒号,则代码将失败。 You may want to raise an exception in such a case, or at least print an error warning perhaps.在这种情况下,您可能希望引发异常,或者至少打印一个错误警告。 You'll also need to use strip in order to remove extraneous whitespaces.您还需要使用strip来删除无关的空格。

Your code also didn't assign to the dictionary -- so I've added that in as well.您的代码也没有分配给字典 - 所以我也添加了它。 Also note -- print returns None , so where you had return print(d) , it would return None .另请注意 - print返回None ,因此在您return print(d)的地方,它将返回None You'll want to return d , so I've updated that as well.你会想要返回d ,所以我也更新了它。

There's one last thing.还有最后一件事。 You probably want to convert the second column of data into float s that can be used as numbers within Python, rather than the strings they would be when read from the file.您可能希望将第二列数据转换为可以用作 Python 中的数字的float ,而不是从文件中读取时的字符串。 In doing so, invalid lines would also raise an exception -- or perhaps you want to want to catch that and handle it differently.这样做时,无效行也会引发异常——或者您可能想要捕获它并以不同的方式处理它。

Try the following:尝试以下操作:

def make_dictionary(file_name):
    d = {}
    with open(file_name, 'r') as file:
        for line in file:
            line = line.strip()
            if line == '':
                # Skip blank lines
                continue
            elif ':' in line:
                # Do the split, since we *know* we can
                key, val = line.split(':')

                # Will raise an exception if the 2nd column is not a valid number
                d[key.strip()] = float(val.strip())
            else:
                # Raise an exception if an invalid line is encountered
                raise ValueError('Cannot split line')
            
    return d

print(make_dictionary('my_text.txt'))

Note: Your original code split the line twice (which is inefficient and unnecessary).注意:您的原始代码将行split两次(这是低效且不必要的)。 In Python, you can do parallel assignment instead (better known as "unpacking") -- I've adapted the code to use that approach.在 Python 中,您可以改为进行并行分配(更好地称为“解包”)——我已经修改了代码以使用该方法。

If you want to make a dictionary, you should use a.json file.如果要制作字典,则应使用 .json 文件。 These were made for storing dictionary like objects.这些是为存储类似对象的字典而设计的。

Your William key has no value.你的威廉钥匙没有价值。
And you have a couple of empty lines.你有几个空行。
So, before you split the line, check if it isn't empty, and after you split check if it's length is 2.因此,在分割线之前,检查它是否不为空,分割后检查它的长度是否为 2。
And, you need to assign something to d in order to return it, eg:而且,您需要为 d 分配一些东西才能返回它,例如:

d[key] = value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM