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