我如何格式化它以使其适合csv格式?我不小心删除了我的表,现在我需要导出这些数据。
+-----+---------------------------------+-------------+-------------------+
| id | email | firstName | lastName |
+-----+---------------------------------+-------------+-------------------+
| 6 | email@email.com | Person1 | Person1 |
| 7 | email2@email.com | Person2 | Person2 |
| 8 | email3@email.com | Person3 | Person3 |
+-----+---------------------------------+-------------+-------------------+
这是我之前制作的文本文件,所以如何删除这些行,使其如下所示:
"6","email@email.com","Person1","Person1"
答案 0 :(得分:1)
您可以在Excel中手动执行此操作(打开文件,数据 - >文本到列,固定宽度。
正则表达式,2个选项:
替换东西:
s/^\|\s+/"/;
s/\s+\|\s+/","/g;
s/\s*\|$/"/;
匹配东西:
s/^\|\s+(\d+)\s+\|\s+(\S+)\s+\|\s+(\S+)\s+\|\s+(\S+)\s+\|$/"$1","$2","$3,"$4"/
当然,确切的语法取决于您使用的语言。
答案 1 :(得分:1)
Javascript中的答案 - RegEx (未提及任何语言,picked JS by looking at your profile
)
[\w\s.@]+(?=\|)
对于特定情况,仅允许\w
,\s
,.
,@
。您可以根据自己的方便允许更多角色
对于多行文字 - TextArea
var str = $('#regex_string').val()
或
对于单行 - Enter
或\n
已删除,使其成为将其存储在变量中的单行
var str = '+-----+---------------------------------+-------------+-------------------+| id | email | firstName | lastName |+-----+---------------------------------+-------------+-------------------+| 6 | email@email.com | Person1 | Person1 || 7 | email2@email.com | Person2 | Person2 || 8 | email3@email.com | Person3 | Person3 |+-----+---------------------------------+-------------+-------------------+'
var extractValidString = str.match(/[\w\s.@]+(?=\|)/g).join("").trim().split(/\s+/)
//output ["id", "email", "firstName", "lastName", "6", "email@email.com", "Person1", "Person1", "7", "email2@email.com", "Person2", "Person2", "8", "email3@email.com", "Person3", "Person3"]
//to remove Headers - id, email, firstName, lastName
extractValidString.splice(0,4)
//extract all rows
var arr = [];
while(extractValidString.length>0) {
//Extract set of 4 elements as you have only 4 columns
//change second no to allow other columns as per your requirement
arr.push(extractValidString.splice(0,4))
}
//output
[Array[4], Array[4], Array[4]]
0: Array[4]
0: "6"
1: "email@email.com"
2: "Person1"
3: "Person1"
length: 4
__proto__: Array[0]
1: Array[4]
0: "7"
1: "email2@email.com"
2: "Person2"
3: "Person2"
length: 4
__proto__: Array[0]
2: Array[4]
0: "8"
1: "email3@email.com"
2: "Person3"
3: "Person3"
length: 4
__proto__: Array[0]
length: 3
__proto__: Array[0]