MongoDB-使用mongoexport

时间:2019-02-14 01:21:38

标签: python mongodb unicode character-encoding mongoexport

我在包含utf8编码的外来字符以及包含mongoexport字符的字段的编码的集合上使用mongoexport似乎正在编码(例如'&')。我注意到的是mongo export对'&'字符进行了unicode转义,但对'ü'等字符进行了转义。这造成了一个问题,因为我试图使用Python读取此数据,但是由于发生了两种不同的编码,因此无法正确地对其进行解码。

例如(使用mongo查询获取记录):

db.Military_Handbooks.findOne({_id: ObjectId("5bf61c80e173a2a10b53ad39")}).PRIMARY_AUTHOR

[
  "Dürer, Albrecht",
  [
    [
      "http://ustc.ac.uk/index.php/search/cicero?tm_fulltext=&tm_field_allauthr=Dürer, Albrecht&tm_translator=&tm_editor=&tm_field_short_title=&tm_field_imprint=&tm_field_place=&sm_field_year=&f_sm_field_year=&t_sm_field_year=&sm_field_country=&sm_field_lang=&sm_field_format=&sm_field_digital=&tm_field_class=&tm_field_cit_name=&tm_field_cit_no=&order=",
      " Dürer, Albrecht"
    ]
  ]
]

运行以下mongoexport命令(如果导出到json,则与此相同):

mongoexport--db ustc --collection Military_Handbooks --type=csv -f=PRIMARY_AUTHOR --limit=1
"[""Dürer, Albrecht"",[[""http://ustc.ac.uk/index.php/search/cicero?tm_fulltext=\u0026tm_field_allauthr=Dürer, Albrecht\u0026tm_translator=\u0026tm_editor=\u0026tm_field_short_title=\u0026tm_field_imprint=\u0026tm_field_place=\u0026sm_field_year=\u0026f_sm_field_year=\u0026t_sm_field_year=\u0026sm_field_country=\u0026sm_field_lang=\u0026sm_field_format=\u0026sm_field_digital=\u0026tm_field_class=\u0026tm_field_cit_name=\u0026tm_field_cit_no=\u0026order="","" Dürer, Albrecht""]]]"

尝试将其读入Python时:

In [24]: import pandas
In [25]: c = pandas.read_csv('Military_Handbooks2.csv')
In [26]: c.at[1, 'PRIMARY_AUTHOR']
Out[26]: '["Dürer, Albrecht",[["http://ustc.ac.uk/index.php/search/cicero?tm_fulltext=\\u0026tm_field_allauthr=Dürer, Albrecht\\u0026tm_translator=\\u0026tm_editor=\\u0026tm_field_short_title=\\u0026tm_field_imprint=\\u0026tm_field_place=\\u0026sm_field_year=\\u0026f_sm_field_year=\\u0026t_sm_field_year=\\u0026sm_field_country=\\u0026sm_field_lang=\\u0026sm_field_format=\\u0026sm_field_digital=\\u0026tm_field_class=\\u0026tm_field_cit_name=\\u0026tm_field_cit_no=\\u0026order="," Dürer, Albrecht"]]]'
In [27]: c.at[1, 'PRIMARY_AUTHOR'].encode().decode('unicode-escape')
Out[27]: '["Dürer, Albrecht",[["http://ustc.ac.uk/index.php/search/cicero?tm_fulltext=&tm_field_allauthr=Dürer, Albrecht&tm_translator=&tm_editor=&tm_field_short_title=&tm_field_imprint=&tm_field_place=&sm_field_year=&f_sm_field_year=&t_sm_field_year=&sm_field_country=&sm_field_lang=&sm_field_format=&sm_field_digital=&tm_field_class=&tm_field_cit_name=&tm_field_cit_no=&order="," Dürer, Albrecht"]]]'

规格:
作业系统:Ubuntu 18.04.1 LTS
的Python:3.6.7
MongoDB Shell版本v3.6.9

1 个答案:

答案 0 :(得分:0)

最后,在忽略错误的同时重新编码文件似乎可以解决问题。

def encoding():
    for fn in os.listdir('.'):
        if '2' not in fn and 'failed' not in fn and 'decode' not in fn:
            try:
                with codecs.open(fn, encoding='utf-8') as fd:
                    text = fd.read()
                    text = text.encode('Windows-1252', errors='ignore').decode('utf-8', errors='ignore')
                with codecs.open(fn[:fn.rfind('.')]+'2.csv', 'w', encoding='utf-8') as fd:
                        fd.write(text)
            except Exception as ex:
                print(ex)
                print('*'*50, '\n')

我还应该指出,我链接到这篇帖子很有帮助:how to export correctly accented words with mongoexport