第三个连续逗号后将逗号更改为冒号 [英] Changing commas into colon after third consecutive commas

查看:126
本文介绍了第三个连续逗号后将逗号更改为冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据先前的问题提出了一个问题 JQuery-csv无法解析所有值,此问题将解决我的问题,但已解决了该问题,但仍需要进行一些调整,以确保溢出的单元格不会出现,但我无法弄清楚从何处开始或如何开始.

Im asking a question based on my previous question JQuery-csv not parsing all values ,this question will be a work around on my problem which was solved but still needed a bit tweaking where the overflowed cell wont show up, but I cant figure out where or how to start with it.

我的csv文件如下所示:test.csv

My csv file looks like this: test.csv

header1, header2, header3, header4
value1, value2, value3, value4
value1, value2, value3, value4.1,value4.2,value4.3
value1, value2, value3, value4

我使用csv.parsers.splitLines解析csv,但是这次我想逐行检查在第三个逗号,之后,我将得到所有剩余的字符串,并将逗号替换为其他分隔符,例如冒号:. 例如:

Im parsing the csv using csv.parsers.splitLines , but this time I would like to check line by line that after the 3rd comma , I'll get all the remaining strings and replace the comma with some other separator like a colon :. ex:

value1, value2, value3, value4.1,value4.2,value4.3 --> value1, value2, value3, value4.1-value4.2-value4.3

此刻,我仍然不知道从哪里开始. 欢呼!!

at the moment i still dont know where to start in this issue a good nudge in this would be awesome. cheers!!

我理想的输出是JSFiddle:

JSFiddle for my ideal output:

http://jsfiddle.net/xpvt214o/690880/

推荐答案

好的,我们开始:

            var separator = ",",
                agregator = "-";
            function generateTable(lines) {
                if (typeof(lines) === 'undefined' || lines.length == 0) {
                    return '';
                }
                var header = lines[0].split(separator);
                var html = '';
                var rows = [];
                // mapping
                for (var row in lines) {
                    if(row == 0) {
                        continue;
                    }
                    var cols = lines[row].split(separator),
                        values = {};
                    for (var col in cols) {
                        var item = header[col] ? header[col] : header[header.length-1];
                        if(values[item]) {
                            values[item].push(cols[col]);
                        } else {
                            values[item] = [cols[col]];
                        }
                    }
                    rows.push(values);
                }
                // printing
                for(var row in rows) {
                    html += '<tr>\r\n';
                    for(var item in rows[row]) {
                        html += '<td>' + item + ':' + rows[row][item].join(agregator) + '</td>\r\n';
                    }
                    html += '</tr>\r\n';
                }
                return html;
            }
            $.ajax({
                type: "GET",
                url: "test.csv",
                dataType: "text",
                success: function(response) {
                    $('#result').html(generateTable($.csv.parsers.splitLines(response)));
                }
            });

正如我在上一个问题中所述,要实现所需的功能,必须先映射这些值,然后再打印这些信息.请注意,为了方便更改,我用新的变量separatoragregator更改了代码. 同样,如果您知道我的意思,那么该溢出解决方案只会将丢失的情况"包装到最后一个标头中. 当您得到最后的答案时,我将只讨论更改.

As I said on the previous question, to achieve what you want you must map those values first, then print those information. Please notice I changed a litle bit our code with new variables separator and agregator just to make it easy to change. Also that overflow solution just wraps the "lost cases" to the last header, if you know what I mean. As you got the last answer I will just talk about the changes.

                for (var row in lines) {
                    if(row == 0) {
                        continue;
                    }
                    var cols = lines[row].split(separator),
                        values = {};
                    for (var col in cols) {
                        var item = header[col] ? header[col] : header[header.length-1];
                        if(values[item]) {
                            values[item].push(cols[col]);
                        } else {
                            values[item] = [cols[col]];
                        }
                    }
                    rows.push(values);
                }

在这里,我将使用简单的对象填充rows,而不是填充html变量.我决定使用一种简单的语法:{headerName: [headerValues]}.因此,每个具有相同名称的标头将位于同一数组上.容易吧?一循环到行,一循环到列,我们就完成了.

Instead of populating the html variable, here I'll populate rows with simple objects. I decided to use a simple syntax: {headerName: [headerValues]}. So each header with the same name will be on the same array. Easy, right? One loop to rows, one loop to cols and we're done.

                for(var row in rows) {
                    html += '<tr>\r\n';
                    for(var item in rows[row]) {
                        html += '<td>' + item + ':' + rows[row][item].join(agregator) + '</td>\r\n';
                    }
                    html += '</tr>\r\n';
                }

因为最难的部分已经去掉了,所以打印更加容易.您只需要再次循环行和列,只需进行语法转换:{headerName:[headerValues]}<td>headerName: [headerValues].join(agregator)</td>.

Printing is more easy because the hardest part has gone. You just need to loop rows and cols again, just making a syntax conversion: {headerName:[headerValues]} to <td>headerName: [headerValues].join(agregator)</td>.

这篇关于第三个连续逗号后将逗号更改为冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆