前端实现CSV文件解析的方法详解

 更新时间:2024年03月19日 11:29:51   作者:每天写一个小BUG  
这篇文章主要为大家详细介绍了前端实现CSV文件解析的相关方法,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

基本规则

简单来说就:使用逗号分隔数据,当出现冲突的使用双引号包裹冲突数据来解决冲突(没有冲突也可以使用双引号包裹数据)。通过逗号将数据分隔成列,通过 \n 换行符将数据分隔成行,因此 CSV 格式可以用来表示二维表格数据。

CSV 解析

根据上面的格式,简单写一个 CSV 解析器。思路其实很简单,就是先按 \n 进行分行,再按 , 进行分列。只不过需要注意的是:在分列的时候遇到 " 要将双引号的内容作为一个整体。具体代码如下:

// 特殊符号枚举
const SIGN = {
  rowDelimiter: '\n',
  colDelimiter: ',',
  specialCharacter: '"',
}
const parseCsv = (str = '') =>
  str.split(SIGN.rowDelimiter).map((row) => {
    const chunk = []
    let doubleQuoteIsClose = true
    let unit = ''
    for (let i = 0; i < row.length; i++) {
      const s = row[i]
      if (s === SIGN.colDelimiter && doubleQuoteIsClose) {
        // 去除首尾 ",这两个双引号可能是由于逗号冲突而添加的
        unit = unit.replace(/^"|"$/g, '')
        chunk.push(unit)
        unit = ''
        continue
      }
      if (s === SIGN.specialCharacter) {
        doubleQuoteIsClose = !doubleQuoteIsClose
      }
      unit += s
    }
    // 收集末尾的 unit
    if (unit) {
      unit = unit.replace(/^"|"$/g, '')
      chunk.push(unit)
    }
    return chunk
  })

通过上面处理,我们可以将一个 csv 格式的数据解析成一个二维数组。其实 csv 数据组织格式的核心很简单:使用 \n 分隔行,使用 , 分隔列,使用 " 作为特殊字符来解决字符冲突。 至于是否组装 header,以及一些优化处理就比较简单了。

PapaParse 源码分析

接下来我们来看一款成熟的工具:PapaParse。PapaParse 有丰富的使用文档,并且在 GitHub 上有 12k 的 star,npm 的周下载量也在非常高的两百多万,是一款比较推荐的 csv 解析库。

解析相关的核心源码我放在了附录。我把它的解析部分分成两个部分,一种是不包含双引号的情况,代码如下:

// Next delimiter comes before next newline, so we've reached end of field
if (
  nextDelim !== -1 &&
  (nextDelim < nextNewline || nextNewline === -1)
) {
  row.push(input.substring(cursor, nextDelim))
  cursor = nextDelim + delimLen
  // we look for next delimiter char
  nextDelim = input.indexOf(delim, cursor)
  continue
}

// End of row
if (nextNewline !== -1) {
  row.push(input.substring(cursor, nextNewline))
  saveRow(nextNewline + newlineLen)

  if (stepIsFunction) {
    doStep()
    if (aborted) return returnable()
  }

  if (preview && data.length >= preview) return returnable(true)

  continue
}

其实也就是简单的按分隔符切割字符串。

另外一种是包含双引号,值得一提的是:如果包含双引号,那么开头和结尾一定是双引号,那么关于双引号中间部分的解析主要需要注意的是内部可能包含转义字符。比如:

// If this quote is escaped, it's part of the data; skip it
// If the quote character is the escape character, then check if the next character is the escape character
if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
  quoteSearch++
  continue
}

// If the quote character is not the escape character, then check if the previous character was the escape character
if (
  quoteChar !== escapeChar &&
  quoteSearch !== 0 &&
  input[quoteSearch - 1] === escapeChar
) {
  continue
}

它的这个逻辑对于转义字符的判断更加细腻,不过我们之前的 double 应该也没有太大的问题。

if (s === SIGN.specialCharacter) {
  doubleQuoteIsClose = !doubleQuoteIsClose
}

所以 csv 数据格式解析的核心逻辑其实是很简单的。

附录(解析相关的源码)

this.parse = function (input, baseIndex, ignoreLastRow) {
  // For some reason, in Chrome, this speeds things up (!?)
  if (typeof input !== 'string') throw new Error('Input must be a string')

  // We don't need to compute some of these every time parse() is called,
  // but having them in a more local scope seems to perform better
  var inputLen = input.length,
    delimLen = delim.length,
    newlineLen = newline.length,
    commentsLen = comments.length
  var stepIsFunction = isFunction(step)

  // Establish starting state
  cursor = 0
  var data = [],
    errors = [],
    row = [],
    lastCursor = 0

  if (!input) return returnable()

  // Rename headers if there are duplicates
  var firstLine
  if (config.header && !baseIndex) {
    firstLine = input.split(newline)[0]
    var headers = firstLine.split(delim)
    var separator = '_'
    var headerMap = new Set()
    var headerCount = {}
    var duplicateHeaders = false

    // Using old-style 'for' loop to avoid prototype pollution that would be picked up with 'var j in headers'
    for (var j = 0; j < headers.length; j++) {
      var header = headers[j]
      if (isFunction(config.transformHeader))
        header = config.transformHeader(header, j)
      var headerName = header

      var count = headerCount[header] || 0
      if (count > 0) {
        duplicateHeaders = true
        headerName = header + separator + count
        // Initialise the variable if it hasn't been.
        if (renamedHeaders === null) {
          renamedHeaders = {}
        }
      }
      headerCount[header] = count + 1
      // In case it already exists, we add more separators
      while (headerMap.has(headerName)) {
        headerName = headerName + separator + count
      }
      headerMap.add(headerName)
      if (count > 0) {
        renamedHeaders[headerName] = header
      }
    }
    if (duplicateHeaders) {
      var editedInput = input.split(newline)
      editedInput[0] = Array.from(headerMap).join(delim)
      input = editedInput.join(newline)
    }
  }
  if (fastMode || (fastMode !== false && input.indexOf(quoteChar) === -1)) {
    var rows = input.split(newline)
    for (var i = 0; i < rows.length; i++) {
      row = rows[i]
      // use firstline as row length may be changed due to duplicated headers
      if (i === 0 && firstLine !== undefined) {
        cursor += firstLine.length
      } else {
        cursor += row.length
      }
      if (i !== rows.length - 1) cursor += newline.length
      else if (ignoreLastRow) return returnable()
      if (comments && row.substring(0, commentsLen) === comments) continue
      if (stepIsFunction) {
        data = []
        pushRow(row.split(delim))
        doStep()
        if (aborted) return returnable()
      } else pushRow(row.split(delim))
      if (preview && i >= preview) {
        data = data.slice(0, preview)
        return returnable(true)
      }
    }
    return returnable()
  }

  var nextDelim = input.indexOf(delim, cursor)
  var nextNewline = input.indexOf(newline, cursor)
  var quoteCharRegex = new RegExp(
    escapeRegExp(escapeChar) + escapeRegExp(quoteChar),
    'g'
  )
  var quoteSearch = input.indexOf(quoteChar, cursor)

  // Parser loop
  for (;;) {
    // Field has opening quote
    if (input[cursor] === quoteChar) {
      // Start our search for the closing quote where the cursor is
      quoteSearch = cursor

      // Skip the opening quote
      cursor++

      for (;;) {
        // Find closing quote
        quoteSearch = input.indexOf(quoteChar, quoteSearch + 1)

        //No other quotes are found - no other delimiters
        if (quoteSearch === -1) {
          if (!ignoreLastRow) {
            // No closing quote... what a pity
            errors.push({
              type: 'Quotes',
              code: 'MissingQuotes',
              message: 'Quoted field unterminated',
              row: data.length, // row has yet to be inserted
              index: cursor,
            })
          }
          return finish()
        }

        // Closing quote at EOF
        if (quoteSearch === inputLen - 1) {
          var value = input
            .substring(cursor, quoteSearch)
            .replace(quoteCharRegex, quoteChar)
          return finish(value)
        }

        // If this quote is escaped, it's part of the data; skip it
        // If the quote character is the escape character, then check if the next character is the escape character
        // 连续两个双引号,表示转译的意思
        if (quoteChar === escapeChar && input[quoteSearch + 1] === escapeChar) {
          quoteSearch++
          continue
        }

        // If the quote character is not the escape character, then check if the previous character was the escape character
        if (
          quoteChar !== escapeChar &&
          quoteSearch !== 0 &&
          input[quoteSearch - 1] === escapeChar
        ) {
          continue
        }

        // 说明匹配到 " 结束符号
        if (nextDelim !== -1 && nextDelim < quoteSearch + 1) {
          nextDelim = input.indexOf(delim, quoteSearch + 1)
        }
        if (nextNewline !== -1 && nextNewline < quoteSearch + 1) {
          nextNewline = input.indexOf(newline, quoteSearch + 1)
        }
        // Check up to nextDelim or nextNewline, whichever is closest
        var checkUpTo =
          nextNewline === -1 ? nextDelim : Math.min(nextDelim, nextNewline)
        var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo)

        // Closing quote followed by delimiter or 'unnecessary spaces + delimiter'
        // 跳过空格
        if (
          input.substr(
            quoteSearch + 1 + spacesBetweenQuoteAndDelimiter,
            delimLen
          ) === delim
        ) {
          row.push(
            input
              .substring(cursor, quoteSearch)
              .replace(quoteCharRegex, quoteChar)
          )
          cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen

          // If char after following delimiter is not quoteChar, we find next quote char position
          if (
            input[
              quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen
            ] !== quoteChar
          ) {
            quoteSearch = input.indexOf(quoteChar, cursor)
          }
          nextDelim = input.indexOf(delim, cursor)
          nextNewline = input.indexOf(newline, cursor)
          break
        }

        var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline)

        // Closing quote followed by newline or 'unnecessary spaces + newLine'
        if (
          input.substring(
            quoteSearch + 1 + spacesBetweenQuoteAndNewLine,
            quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen
          ) === newline
        ) {
          row.push(
            input
              .substring(cursor, quoteSearch)
              .replace(quoteCharRegex, quoteChar)
          )
          saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen)
          nextDelim = input.indexOf(delim, cursor) // because we may have skipped the nextDelim in the quoted field
          quoteSearch = input.indexOf(quoteChar, cursor) // we search for first quote in next line

          if (stepIsFunction) {
            doStep()
            if (aborted) return returnable()
          }

          if (preview && data.length >= preview) return returnable(true)

          break
        }

        // Checks for valid closing quotes are complete (escaped quotes or quote followed by EOF/delimiter/newline) -- assume these quotes are part of an invalid text string
        errors.push({
          type: 'Quotes',
          code: 'InvalidQuotes',
          message: 'Trailing quote on quoted field is malformed',
          row: data.length, // row has yet to be inserted
          index: cursor,
        })

        quoteSearch++
        continue
      }

      continue
    }

    // Comment found at start of new line
    if (
      comments &&
      row.length === 0 &&
      input.substring(cursor, cursor + commentsLen) === comments
    ) {
      if (nextNewline === -1)
        // Comment ends at EOF
        return returnable()
      cursor = nextNewline + newlineLen
      nextNewline = input.indexOf(newline, cursor)
      nextDelim = input.indexOf(delim, cursor)
      continue
    }

    // Next delimiter comes before next newline, so we've reached end of field
    if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1)) {
      row.push(input.substring(cursor, nextDelim))
      cursor = nextDelim + delimLen
      // we look for next delimiter char
      nextDelim = input.indexOf(delim, cursor)
      continue
    }

    // End of row
    if (nextNewline !== -1) {
      row.push(input.substring(cursor, nextNewline))
      saveRow(nextNewline + newlineLen)

      if (stepIsFunction) {
        doStep()
        if (aborted) return returnable()
      }

      if (preview && data.length >= preview) return returnable(true)

      continue
    }

    break
  }

  return finish()

  function pushRow(row) {
    data.push(row)
    lastCursor = cursor
  }

  /**
   * checks if there are extra spaces after closing quote and given index without any text
   * if Yes, returns the number of spaces
   */
  function extraSpaces(index) {
    var spaceLength = 0
    if (index !== -1) {
      var textBetweenClosingQuoteAndIndex = input.substring(
        quoteSearch + 1,
        index
      )
      if (
        textBetweenClosingQuoteAndIndex &&
        textBetweenClosingQuoteAndIndex.trim() === ''
      ) {
        spaceLength = textBetweenClosingQuoteAndIndex.length
      }
    }
    return spaceLength
  }

  /**
   * Appends the remaining input from cursor to the end into
   * row, saves the row, calls step, and returns the results.
   */
  function finish(value) {
    if (ignoreLastRow) return returnable()
    if (typeof value === 'undefined') value = input.substring(cursor)
    row.push(value)
    cursor = inputLen // important in case parsing is paused
    pushRow(row)
    if (stepIsFunction) doStep()
    return returnable()
  }

  /**
   * Appends the current row to the results. It sets the cursor
   * to newCursor and finds the nextNewline. The caller should
   * take care to execute user's step function and check for
   * preview and end parsing if necessary.
   */
  function saveRow(newCursor) {
    cursor = newCursor
    pushRow(row)
    row = []
    nextNewline = input.indexOf(newline, cursor)
  }

  /** Returns an object with the results, errors, and meta. */
  function returnable(stopped) {
    return {
      data: data,
      errors: errors,
      meta: {
        delimiter: delim,
        linebreak: newline,
        aborted: aborted,
        truncated: !!stopped,
        cursor: lastCursor + (baseIndex || 0),
        renamedHeaders: renamedHeaders,
      },
    }
  }

  /** Executes the user's step function and resets data & errors. */
  function doStep() {
    step(returnable())
    data = []
    errors = []
  }
}

到此这篇关于前端实现CSV文件解析的方法详解的文章就介绍到这了,更多相关CSV文件解析内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

  • 微信小程序向Java后台传输参数的方法实现

    微信小程序向Java后台传输参数的方法实现

    这篇文章主要介绍了微信小程序向Java后台传输参数的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • JavaScript?数组方法filter与reduce

    JavaScript?数组方法filter与reduce

    这篇文章主要介绍了JavaScript?数组方法filter与reduce,在ES6新增的数组方法中,包含了多个遍历方法,其中包含了用于筛选的filter和reduce
    2022-07-07
  • 一个层慢慢增高展开,有种向下滑动的效果

    一个层慢慢增高展开,有种向下滑动的效果

    一个层慢慢增高展开,有种向下滑动的效果...
    2006-11-11
  • javascript实现编写网页版计算器

    javascript实现编写网页版计算器

    这篇文章主要为大家详细介绍了javascript实现编写网页版计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 拆开JavaScript迭代器模式内部黑盒子

    拆开JavaScript迭代器模式内部黑盒子

    这篇文章主要为大家介绍了JavaScript迭代器模式内部黑盒子解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • JS面试题---关于算法台阶的问题

    JS面试题---关于算法台阶的问题

    下面小编就为大家带来一篇JS面试题---关于算法台阶的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • 在网页中使用document.write时遭遇的奇怪问题

    在网页中使用document.write时遭遇的奇怪问题

    很多时候我们都会在网页上的JavaScript中使用document.write来写入一些东西,有的时候可能因为我们无法改变某一部分HTML而不得不使用这样的办法,也有的时候是因为在进行跨应用的脚本调用。
    2010-08-08
  • Javascript函数的参数

    Javascript函数的参数

    本文给大家分享的是网易云课堂中金旭亮老师的课堂笔记,对于大家学习javascript非常有帮助,这里推荐给小伙伴们
    2015-07-07
  • 表单切换,用回车键替换Tab健(不支持IE)

    表单切换,用回车键替换Tab健(不支持IE)

    表单切换,用回车键替换Tab健。input的属性tab的值表示切换的顺序,这个值必须是连续的,并且不能重复。目前不支持IE
    2011-07-07
  • Safari5中alert的无限循环BUG

    Safari5中alert的无限循环BUG

    猜测Safari5中将点击alert框的确定按钮也当成点击body了。事件一直冒泡到弹出框上。
    2011-04-04

最新评论

?


http://www.vxiaotou.com