【问题标题】:Exporting data from Cloudant/CouchDB to CSV将数据从 Cloudant/CouchDB 导出到 CSV
【发布时间】:2019-09-09 14:55:03
【问题描述】:

从 Cloudant 将数据导出到 CSV 时遇到一些小问题。当前使用此处找到的 CSV 函数:https://developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-or-ical/

问题是一些数据后来添加了 2-3 个字段。当它下载文档时,它只是将信息一个接一个地放置,并且它不能解释一些旧数据丢失的字段,因此数据会错位。

我尝试创建函数来尝试检测该字段是否存在,以及它是否没有将其设置为空字符串。

这是我尝试过的,它给了我错误:{"error":"compilation_error","re​​ason":"Expression does not eval to a function.


 // output HTTP headers
 start({
   headers: {  'Content-Type': 'text/csv'  },
 });


 // iterate through the result set
 while(row = getRow()) {

   // get the doc (include_docs=true)
   var doc = row.doc;

   // if this is the first row
   if (first) {

     // output column headers
     send(Object.keys(doc).join(',') + 'n');
     first = false;
   }

   // build up a line of output
   var line = '';

   // iterate through each row
   //for(var i in doc) {

     if (doc.hasOwnProperty('customerNumber')) {
     } else {
       doc.customerNumber = '';
     }

     if (doc.hasOwnProperty('affiliateNumber')) {
     } else {
       doc.affiliateNumber = '';
     }

     if (doc.hasOwnProperty('dunsNumber')) {
     } else {
       doc.dunsNumber = '';
     }

     if (doc.hasOwnProperty('dunsDomestic')) {
     } else {
       doc.dunsDomestic = '';
     }

     if (doc.hasOwnProperty('dunsGlobal')) {
     } else {
       doc.dunsGlobal = '';
     }

     if (doc.hasOwnProperty('countryCode')) {
     } else {
       doc.countryCode = '';
     }

     if (doc.hasOwnProperty('companyName')) {
     } else {
       doc.companyName = '';
     }

     if (doc.hasOwnProperty('countryPreapprovedAmt')) {
     } else {
       doc.countryPreapprovedAmt = '';
     }

     if (doc.hasOwnProperty('preapprovedAmt')) {
     } else {
       doc.preapprovedAmt = '';
     }

     if (doc.hasOwnProperty('currency')) {
     } else {
       doc.currency = '';
     }

     if (doc.hasOwnProperty('expirationDate')) {
     } else {
       doc.expirationDate = '';
     }

     if (doc.hasOwnProperty('blacklist')) {
     } else {
       doc.blacklist = '';
     }

     line += doc.customerNumber + ',' + doc.affiliateNumber + ',' + doc.dunsNumber+ ',' + doc.dunsDomestic+ ',' + doc.dunsGlobal+ ',' + doc.countryCode+ ',' + doc.companyName+ ',' + doc.countryPreapprovedAmt+ ',' + doc.preapprovedAmt+ ',' + doc.currency+ ',' + doc.expirationDate+ ',' + doc.blacklist;
   //}
   line += 'n';

   // send  the line
   send(line);
 }
};


当它遇到不包含所有这些字段的数据时,它会检测到它。分配一个空字符串,以便在下载 csv 时数据对齐。

【问题讨论】:

  • 嗨@user3413540。您的问题到底是什么,我们能提供什么帮助?
  • 在将 jsons 导出为 csv 时,假设 1 条数据有 10 个字段。它将一个接一个地连续放置每条数据。虽然有一些较旧的数据有 7-8 个字段,但当它一一放置时,它在 csv 文件中不对齐。我正在寻找一种方法来检测是否缺少字段,用空格填充它们以正确对齐所有内容。
  • 我认为格林的回答应该符合这个描述。

标签: javascript csv couchdb cloudant


【解决方案1】:

如果您想从 CouchDB/Cloudant 中相对扁平的文档生成 CSV 文件,您有两种选择:

  1. 使用 CouchDB“列表”函数,如您链接的博文中所述。
  2. 使用实用程序将数据导出为 JSON 并在客户端转换为 CSV

选项 2 可以通过与 couchexport 配套工具一起提供的开源 couchimport 实用程序来实现:

数据在命令行上导出,并且可以通过管道传输到文件以进行进一步分析。

couchexport --db mydb > mydb.csv

如果文档的格式不正确,可以提供“过滤”功能来强制将数据转换为正确的格式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2020-09-11
    相关资源
    最近更新 更多