我需要打印具有不同字段的不同字典,ValueError:dict包含不在字段名称中的字段:

时间:2019-06-13 11:29:08

标签: python csv dictionary

我有4个字典,某些字段在所有4个字典中都不存在。我收到此错误

  

ValueError:dict包含不在字段名称中的字段:'from','Command'

阅读一些文档后,我发现DictWriter的 extrasaction ='ignore'参数,但在CSV文件中却保留了一些字段。 我需要CSV文件中的所有字段,即使某些字典中的字段为空。据我了解,它只是打印公共字段。

词典是:

  

{'Command':'DELETE','table':'abc','from':'abc','where_expr':'c = book'}

     

{'Command':'SELECT','columns':'a b','table':'tab1','from':'tab1'}

     

{'Command':'INSERT','table':'xyz','into':'xyz','columns':'铅笔笔','值':'200 20'}

     

{'Command':'UPDATE','table':'Student','columns':'NAME','values':'PRATIK','where_expr':'a = 100'}

由于错误,我包括了多余的行为。

with open('del.csv', 'w') as f:  
    write = csv.DictWriter(f, dict4.keys(),extrasaction='ignore') 
    write.writeheader()
    write.writerow(dict5)
    write.writerow(dict6)
    write.writerow(dict7)
    write.writerow(dict8)

输出看起来像: (我使用逗号分隔字段。空白表示空白字段(它们没有值))

  

表|列|值| where_expr

     

abc,,,c = book

     

tab1,a b,,

     

xyz,铅笔笔,200 20

     

学生,姓名,PRATIK,a = 100

已编辑:所需的输出是: (空格表示空白字段),我希望可以发布CSV文件。

  

Command | Table | Columns | From | Where_expr | Into | Values

     

DELETE,abc,,abc,c = book,,,

     

SELECT,tab1,a b,tab1,,,,

     

INSERT,xyz,铅笔,``,xyz,200 20

     

UPDATE,student,name,a = 100,PRATIK

2 个答案:

答案 0 :(得分:1)

您可以使用pandas数据框将所有词典加载到列表中,并从该数据框制作csv文件。试试这个:

jekyll serve --port

输出(CSV文件内容):

   Source: /home/a__/repos/Bxgxaxdx.github.io
       Destination: /home/a__/repos/Bxgxaxdx.github.io/_site
 Incremental build: disabled. Enable with --incremental
      Generating... 
                    done in 0.029 seconds.
Traceback (most recent call last):
    32: from /usr/bin/jekyll:11:in `<main>'
    31: from /usr/lib/ruby/vendor_ruby/mercenary.rb:19:in `program'
    30: from /usr/lib/ruby/vendor_ruby/mercenary/program.rb:42:in `go'
    29: from /usr/lib/ruby/vendor_ruby/mercenary/command.rb:220:in `execute'
    28: from /usr/lib/ruby/vendor_ruby/mercenary/command.rb:220:in `each'
    27: from /usr/lib/ruby/vendor_ruby/mercenary/command.rb:220:in `block in execute'
    26: from /usr/lib/ruby/vendor_ruby/jekyll/commands/serve.rb:34:in `block (2 levels) in init_with_program'
    25: from /usr/lib/ruby/vendor_ruby/jekyll/commands/build.rb:39:in `process'
    24: from /usr/lib/ruby/vendor_ruby/jekyll/commands/build.rb:72:in `watch'
    23: from /usr/lib/ruby/vendor_ruby/jekyll/watcher.rb:10:in `watch'
    22: from /usr/lib/ruby/vendor_ruby/listen/listener.rb:91:in `start'
    21: from /usr/lib/ruby/vendor_ruby/listen/fsm.rb:57:in `transition'
    20: from /usr/lib/ruby/vendor_ruby/listen/fsm.rb:91:in `transition_with_callbacks!'
    19: from /usr/lib/ruby/vendor_ruby/listen/fsm.rb:121:in `call'
    18: from /usr/lib/ruby/vendor_ruby/listen/fsm.rb:121:in `instance_eval'
    17: from /usr/lib/ruby/vendor_ruby/listen/listener.rb:68:in `block in <class:Listener>'
    16: from /usr/lib/ruby/2.6.0/forwardable.rb:230:in `start'
    15: from /usr/lib/ruby/vendor_ruby/listen/adapter/base.rb:63:in `start'
    14: from /usr/lib/ruby/vendor_ruby/listen/adapter/base.rb:40:in `configure'
    13: from /usr/lib/ruby/vendor_ruby/listen/adapter/base.rb:40:in `each'
    12: from /usr/lib/ruby/vendor_ruby/listen/adapter/base.rb:45:in `block in configure'
    11: from /usr/lib/ruby/vendor_ruby/listen/adapter/linux.rb:30:in `_configure'
    10: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     9: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     8: from /usr/lib/ruby/vendor_ruby/rb-inotify.rb:1:in `<top (required)>'
     7: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     6: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     5: from /usr/lib/ruby/vendor_ruby/rb-inotify/native.rb:1:in `<top (required)>'
     4: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     3: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     2: from /usr/lib/ruby/vendor_ruby/ffi.rb:4:in `<top (required)>'
     1: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': **cannot load such file -- 2.6/ffi_c (LoadError)**
    33: from /usr/bin/jekyll:11:in `<main>'
    32: from /usr/lib/ruby/vendor_ruby/mercenary.rb:19:in `program'
    31: from /usr/lib/ruby/vendor_ruby/mercenary/program.rb:42:in `go'
    30: from /usr/lib/ruby/vendor_ruby/mercenary/command.rb:220:in `execute'
    29: from /usr/lib/ruby/vendor_ruby/mercenary/command.rb:220:in `each'
    28: from /usr/lib/ruby/vendor_ruby/mercenary/command.rb:220:in `block in execute'
    27: from /usr/lib/ruby/vendor_ruby/jekyll/commands/serve.rb:34:in `block (2 levels) in init_with_program'
    26: from /usr/lib/ruby/vendor_ruby/jekyll/commands/build.rb:39:in `process'
    25: from /usr/lib/ruby/vendor_ruby/jekyll/commands/build.rb:72:in `watch'
    24: from /usr/lib/ruby/vendor_ruby/jekyll/watcher.rb:10:in `watch'
    23: from /usr/lib/ruby/vendor_ruby/listen/listener.rb:91:in `start'
    22: from /usr/lib/ruby/vendor_ruby/listen/fsm.rb:57:in `transition'
    21: from /usr/lib/ruby/vendor_ruby/listen/fsm.rb:91:in `transition_with_callbacks!'
    20: from /usr/lib/ruby/vendor_ruby/listen/fsm.rb:121:in `call'
    19: from /usr/lib/ruby/vendor_ruby/listen/fsm.rb:121:in `instance_eval'
    18: from /usr/lib/ruby/vendor_ruby/listen/listener.rb:68:in `block in <class:Listener>'
    17: from /usr/lib/ruby/2.6.0/forwardable.rb:230:in `start'
    16: from /usr/lib/ruby/vendor_ruby/listen/adapter/base.rb:63:in `start'
    15: from /usr/lib/ruby/vendor_ruby/listen/adapter/base.rb:40:in `configure'
    14: from /usr/lib/ruby/vendor_ruby/listen/adapter/base.rb:40:in `each'
    13: from /usr/lib/ruby/vendor_ruby/listen/adapter/base.rb:45:in `block in configure'
    12: from /usr/lib/ruby/vendor_ruby/listen/adapter/linux.rb:30:in `_configure'
    11: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    10: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     9: from /usr/lib/ruby/vendor_ruby/rb-inotify.rb:1:in `<top (required)>'
     8: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     7: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     6: from /usr/lib/ruby/vendor_ruby/rb-inotify/native.rb:1:in `<top (required)>'
     5: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     4: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     3: from /usr/lib/ruby/vendor_ruby/ffi.rb:3:in `<top (required)>'
     2: from /usr/lib/ruby/vendor_ruby/ffi.rb:6:in `rescue in <top (required)>'
     1: from /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': **cannot load such file -- ffi_c (LoadError)**

答案 1 :(得分:1)

使用set.union操作(对于动态列数的情况):

import csv

dict5 = {'Command': 'DELETE', 'table': 'abc', 'from': 'abc', 'where_expr': 'c = book'}
dict6 = {'Command': 'SELECT', 'columns': 'a b', 'table': 'tab1', 'from': 'tab1'}
dict7 = {'Command': 'INSERT', 'table': 'xyz', 'into': 'xyz', 'columns': 'pencil pens', 'values': '200 20'}
dict8 = {'Command': 'UPDATE', 'table': 'Student', 'columns': 'NAME', 'values': 'PRATIK', 'where_expr': 'a = 100'}

with open('names.csv', 'w', newline='') as csvfile:
    # `initial_fieldnames` are your dict4 keys
    initial_fieldnames = ["table", "columns", "values", "where_expr"]
    fieldnames = sorted(set(initial_fieldnames).union(*[dict5, dict6, dict7, dict8]))
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore')

    writer.writeheader()
    writer.writerows([dict5, dict6, dict7, dict8])

names.csv内容:

Command,columns,from,into,table,values,where_expr
DELETE,,abc,,abc,,c = book
SELECT,a b,tab1,,tab1,,
INSERT,pencil pens,,xyz,xyz,200 20,
UPDATE,NAME,,,Student,PRATIK,a = 100