javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > CSV文件解析

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

作者:每天写一个小BUG

这篇文章主要为大家详细介绍了前端实现CSV文件解析的相关方法,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解一下

基本规则

简单来说就:使用逗号分隔数据,当出现冲突的使用双引号包裹冲突数据来解决冲突(没有冲突也可以使用双引号包裹数据)。通过逗号将数据分隔成列,通过 \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文件解析内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文