如果没有密钥,则保持dict的初始化值

时间:2016-08-17 09:50:35

标签: python python-3.x

我正在尝试解析pdf元数据,如:

    fields = ["Author", "Year",  "Journal", "Title", "Publisher",
                   "Page", "Address", "Annote", "Booktitle", "Chapter",
                   "Crossred", "Edition", "Editor", "HowPublished",
                   "Institution", "Month", "Note", "Number",
                   "Organization", "Pages", "School",
                   "Series", "Type", "Volume", "Doi", "File"]
    op=pexif.get_json(filename)
    new_op = {"Author":"Unknown"}
    print(new_op)
    new_op = {
        field: str(value) for field in fields
        for key, value in op[0].items() if field.lower() in key.lower()
    }
    print(new_op)
    id_auth=new_op["Author"].split()[-1]
    id_tit = (new_op["Title"].split()[:2])

在少数情况下,Author标签不存在,因此我使用Unknown对其进行初始化,希望如果找不到Author标记,则值将持续。 但是,在new_op ={}中,它会覆盖旧数据。因此,对于两个print(new_op)产量:

{'Author': 'Unknown'}
{'File': '/home/rudra/Downloads', 'Title': 'Formation of bcc non-equilibrium La, Gd and Dy alloys and the magnetic structure of Mg-stabilized [beta] Gd and [beta] Dy', 'Type': 'pdf', 'Page': '140'}

并为id_auth行抛出一个KeyError:

id_auth=new_op["Author"].split()[-1]
KeyError: 'Author'

如果op中没有Author键,我试图保持Author = Unknow。 我怎么能这样做?

供参考,以下是exiftool输出:

ExifTool Version Number         : 10.20
File Name                       : Formation of bcc non-equilibrium La Gd and Dy alloys and the mag.pdf
Directory                       : /home/rudra/Downloads
File Size                       : 2.2 MB
File Modification Date/Time     : 2016:07:20 15:30:48+02:00
File Access Date/Time           : 2016:08:16 19:20:21+02:00
File Inode Change Date/Time     : 2016:08:16 18:13:30+02:00
File Permissions                : rw-rw-r--
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.7
Linearized                      : No
XMP Toolkit                     : Adobe XMP Core 5.2-c001 63.143651, 2012/04/05-09:01:49
Modify Date                     : 2015:09:18 07:48:48-07:00
Create Date                     : 2015:09:18 07:48:48-07:00
Metadata Date                   : 2015:09:18 07:48:48-07:00
Creator Tool                    : Appligent AppendPDF Pro 5.5
Document ID                     : uuid:f06a868b-a105-11b2-0a00-782dad000000
Instance ID                     : uuid:f06aec42-a105-11b2-0a00-400080adfd7f
Format                          : application/pdf
Title                           : Formation of bcc non-equilibrium La, Gd and Dy alloys and the magnetic structure of Mg-stabilized [beta] Gd and [beta] Dy
Producer                        : Prince 9.0 rev 5 (www.princexml.com)
Appligent                       : AppendPDF Pro 5.5 Linux Kernel 2.6 64bit Oct  2 2014 Library 10.1.0
Page Count                      : 140
Creator                         : Appligent AppendPDF Pro 5.5

3 个答案:

答案 0 :(得分:1)

有很多方法可以解决这个问题,但最简单的方法是删除dict的初始版本,然后检查作者是否存在:

new_op = {
    field: str(value) for field in fields
    for key, value in op[0].items() if field.lower() in key.lower()
}
if 'Author' not in new_op:
    new_op['Author'] = 'Unknown'

答案 1 :(得分:0)

您正在重新分配rustdoc词典。相反,在完成以下任务后

404

这样做:

new_op

答案 2 :(得分:0)

new_op = {
    field: str(value) for field in fields
    for key, value in op[0].items() if field.lower() in key.lower()
}