将文本文件转换为表格

时间:2018-07-24 02:57:08

标签: javascript arrays multiple-columns

我有一个文本文件,其中包含要尝试以表格格式显示的代码。第一栏应该是水果的类型,第二栏应该是食用的数量,第三栏应该是购买的数量,第四栏是购买的日期和时间。

我尝试解析数据并将其记录到控制台。这似乎可以更好地可视化数据,但是老实说,我不知道如何在表中实际显示数据。我真的是Java编码新手,所以我不太了解该语言。

这是我在该网站上的第一篇文章,因此我不确定是否可以正确发布。

link to代码:https://pastebin.com/PxYASR2T

2 个答案:

答案 0 :(得分:0)

您可以将数据解析为可用的格式,如下所示,可以通过多种方式将其安装为表格,例如https://www.npmjs.com/package/tableifyConvert JSON array to an HTML table in jQuery

const parsedData = getData()
  .split('\n')
  .filter(x => !!x && !x.match(/x{2,}/))
  .map(fruitRow => {
    const thisSplit = fruitRow.split(/[-–-]/)
    return {
      fruit: thisSplit[3].replace(/\s+/g, ''),
      eaten: thisSplit[4].match(/\d+/)[0],
      bought: thisSplit[6].replace('\n', '')
    }
  })
  .filter(x => !!x)
  
console.dir(parsedData)

function getData() {
  return data = `
2018-07-23 07:33:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 1- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 07:43:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 2- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 08:03:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 2- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 08:53:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 3- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 09:03:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 3- No of fruit bought –1
2018-07-23 09:03:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 1- No of fruit bought –0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 09:23:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 3- No of fruit bought –1
2018-07-23 09:23:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 1- No of fruit bought –0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 09:33:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 4- No of fruit bought –0
2018-07-23 09:33:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 2- No of fruit bought –0
2018-07-23 09:33:38 INFO  TestJsonSyncUtil:237  - Strawberry – No of fruit eaten 0- No of fruit bought –1
2018-07-23 09:33:38 INFO  TestJsonSyncUtil:237  - Mango – No of fruit eaten 0- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 09:43:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 4- No of fruit bought –0
2018-07-23 09:43:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 4- No of fruit bought –2
2018-07-23 09:43:38 INFO  TestJsonSyncUtil:237  - Strawberry – No of fruit eaten 0- No of fruit bought –1
2018-07-23 09:43:38 INFO  TestJsonSyncUtil:237  - Mango – No of fruit eaten 0- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 09:53:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 4- No of fruit bought –0
2018-07-23 09:53:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 5- No of fruit bought –1
2018-07-23 09:53:38 INFO  TestJsonSyncUtil:237  - Strawberry – No of fruit eaten 0- No of fruit bought –1
2018-07-23 09:53:38 INFO  TestJsonSyncUtil:237  - Mango – No of fruit eaten 0- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 10:03:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 4- No of fruit bought –0
2018-07-23 10:03:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 6- No of fruit bought –1
2018-07-23 10:03:38 INFO  TestJsonSyncUtil:237  - Strawberry – No of fruit eaten 0- No of fruit bought –1
2018-07-23 10:03:38 INFO  TestJsonSyncUtil:237  - Mango – No of fruit eaten 0- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 10:13:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 4- No of fruit bought –0
2018-07-23 10:13:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 9- No of fruit bought –2
2018-07-23 10:13:38 INFO  TestJsonSyncUtil:237  - Strawberry – No of fruit eaten 0- No of fruit bought –1
2018-07-23 10:13:38 INFO  TestJsonSyncUtil:237  - Mango – No of fruit eaten 0- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 10:23:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 4- No of fruit bought –0
2018-07-23 10:23:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 9- No of fruit bought –1
2018-07-23 10:23:38 INFO  TestJsonSyncUtil:237  - Strawberry – No of fruit eaten 0- No of fruit bought –1
2018-07-23 10:23:38 INFO  TestJsonSyncUtil:237  - Mango – No of fruit eaten 0- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 10:33:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 4- No of fruit bought –0
2018-07-23 10:33:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 10- No of fruit bought –1
2018-07-23 10:33:38 INFO  TestJsonSyncUtil:237  - Strawberry – No of fruit eaten 0- No of fruit bought –1
2018-07-23 10:33:38 INFO  TestJsonSyncUtil:237  - Mango – No of fruit eaten 0- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2018-07-23 10:43:38 INFO  TestJsonSyncUtil:237  - Apple – No of fruit eaten 10- No of fruit bought –3
2018-07-23 10:43:38 INFO  TestJsonSyncUtil:237  - Banana – No of fruit eaten 10- No of fruit bought –0
2018-07-23 10:43:38 INFO  TestJsonSyncUtil:237  - Strawberry – No of fruit eaten 0- No of fruit bought –1
2018-07-23 10:43:38 INFO  TestJsonSyncUtil:237  - Mango – No of fruit eaten 0- No of fruit bought –1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
`;
}

答案 1 :(得分:-1)

您需要将文本转换为JSON格式,然后可以使用AJAX Call将JSON文本轻松呈现到表中。