首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Protovis -处理文本源

Protovis -处理文本源
EN

Stack Overflow用户
提问于 2011-08-05 04:08:18
回答 1查看 306关注 0票数 0

假设我有一个文本文件,其中的行如下:

代码语言:javascript
复制
[4/20/11 17:07:12:875 CEST] 00000059 FfdcProvider  W com.test.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I: FFDC Incident emitted on D:/Prgs/testing/WebSphere/AppServer/profiles/ProcCtr01/logs/ffdc/server1_3d203d20_11.04.20_17.07.12.8755227341908890183253.txt com.test.testserver.management.cmdframework.CmdNotificationListener 134
[4/20/11 17:07:27:609 CEST] 0000005d wle           E   CWLLG2229E: An exception occurred in an EJB call.  Error: Snapshot with ID Snapshot.8fdaaf3f-ce3f-426e-9347-3ac7e8a3863e not found.
                             com.lombardisoftware.core.TeamWorksException: Snapshot with ID Snapshot.8fdaaf3f-ce3f-426e-9347-3ac7e8a3863e not found.
   at com.lombardisoftware.server.ejb.persistence.CommonDAO.assertNotNull(CommonDAO.java:70)

有没有办法轻松地将这样的数据源导入到protovis中,如果没有,那么将其解析为JSON格式的最简单方法是什么?例如,第一个条目的解析可能如下所示:

代码语言:javascript
复制
[
  {
 "Date": "4/20/11 17:07:12:875 CEST",
 "Status": "00000059",
 "Msg": "FfdcProvider  W com.test.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I",
 },
]

谢谢,大卫

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-06 01:38:24

Protovis本身不提供解析文本文件的任何实用程序,因此您可以选择:

JSON

  • 使用Javascript将文本解析为对象,很可能是使用文本解析语言或您选择的实用程序将文本导出为文件。

您选择哪一个取决于以下几个因素:

  • 的数据是静态的,还是每次查看时都要在新的或动态的文件上运行?对于静态数据,预处理可能是最简单的;对于动态数据,这可能会增加一个恼人的额外步骤。
  • 你有多少数据?在Javascript中解析20K的文本文件完全可以;解析2MB的文件将非常慢,并且会导致浏览器在工作时挂起(除非您使用Worker)。
  • 如果涉及大量处理,您更愿意将负载放在服务器上(通过使用服务器端脚本进行预处理)还是放在客户端(通过在浏览器中执行)?

如果您希望在Javascript中根据您提供的示例执行此操作,则可以执行以下操作:

代码语言:javascript
复制
// Assumes var text = 'your text';
// use the utility of your choice to load your text file into the
// variable (e.g. jQuery.get()), or just paste it in.
var lines = text.split(/[\r\n\f]+/),
    // regex to match your log entry beginning
    patt = /^\[(\d\d?\/\d\d?\/\d\d? \d\d:\d\d:\d\d:\d{3} [A-Z]+)\] (\d{8})/,
    items = [],
    currentItem;

// loop through the lines in the file
lines.forEach(function(line) {
    // look for the beginning of a log entry
    var initialData = line.match(patt);
    if (initialData) {
        // start a new item, using the captured matches
        currentItem = {
            Date: initialData[1],
            Status: initialData[2],
            Msg: line.substr(initialData[0].length + 1)
        }
        items.push(currentItem);
    } else {
        // this is a continuation of the last item
        currentItem.Msg += "\n" + line;  
    }
});

// items now contains an array of objects with your data
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6947890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档