简体   繁体   English

如何将我的元素转换为字典

[英]how transform my elements into a dictionnary

I try to do some templates and I need to create something like that:我尝试做一些模板,我需要创建类似的东西:

issuer: QualityHosting AG
fields:
  amount: Total EUR\s+(\d+,\d+)
  amount_untaxed: Total EUR\s+(\d+,\d+)
  date:
    - \s{2,}(\d+\. .+ \d{4})\s{2,}
    - Rechnungsdatum\s+(\w+ \d+, \d{4})
  invoice_number: Rechnungsnr\.\s+(\d{8})

Its not a problem with the regex but with the dict;这不是正则表达式的问题,而是字典的问题; how can I have fields:amount:我怎样才能有字段:金额:

there is my code but I don't think its a correct way有我的代码,但我认为它不是正确的方法

   
    issuer = request.form['issuer']
    amount = request.form['amount']
    amount_untaxed = request.form['amount_untaxed']
    date =  request.form['date']
    invoice_number = request.form['invoice_number']
    start= request.form['start']
    keyword= request.form['keyword']
    print(issuer, amount,date, amount_untaxed, invoice_number, start, keyword)

    di={
        "issuer": issuer,
        "fields" : {
        "amount": amount,
        "amount": "1964",
        "date": date
        }
    }
    print(di.fiel)

I searched on tutorials but it was not exactly this.我搜索了教程,但不完全是这个。

First, the keys of a dictionary must be unique, so you can't have 2 amount as key.首先,字典的键必须是唯一的,所以不能有 2 个数量作为键。

Second, to get the key value you use the function get(key_value)其次,要获取键值,请使用 function get(key_value)

di={
    "issuer": 'issuer',
    "fields" : {
    "amount1": 'amount',
    "amount2": "1964",
    "date": 'date'
    }
}
print(di.get("fields"))
# {'amount1': 'amount', 'amount2': '1964', 'date': 'date'}

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

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