Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将换行分隔的半结构化数据转换为结构化数据_Python_Bash_Text - Fatal编程技术网

Python 如何将换行分隔的半结构化数据转换为结构化数据

Python 如何将换行分隔的半结构化数据转换为结构化数据,python,bash,text,Python,Bash,Text,我有一个列表,其中每个记录都由一条空行分隔: P http://codeproject.com/kb/silverlight/convertsilverlightcontrol.aspx T 2008-08-01 00:00:00 Q how to create property binding in a visual webgui silverlight control Q videoplayer silverlight controls videoplayer videoplayer sil

我有一个列表,其中每个记录都由一条空行分隔:

P http://codeproject.com/kb/silverlight/convertsilverlightcontrol.aspx
T 2008-08-01 00:00:00
Q how to create property binding in a visual webgui silverlight control
Q videoplayer silverlight controls videoplayer videoplayer silverlight controls version 1 0 0 0 culture neutral publickeytoken null
Q videoplayer controls videoplayer videoplayer controls

P http://wallstreetexaminer.com/?p=2987
T 2008-08-01 00:00:01

P http://news.bbc.co.uk/go/rss/-/1/hi/scotland/highlands_and_islands/7535558.stm
T 2008-08-01 00:00:01
Q our continuing strategic priority is to provide a safe and efficient group of airports while pursuing development opportunities which improve the air transport network serving the region
Q our results for the year demonstrate that we have delivered against these targets and ensured that our airports have continued to play a central role in the economic and social life of the highlands and islands and tayside

P http://news.bbc.co.uk/go/rss/-/1/hi/scotland/south_of_scotland/7535392.stm
T 2008-08-01 00:00:01
Q safeguard our fishing communities birthright for future generations
Q every time i visit a fishing community in scotland i am asked to take steps to protect fishing rights for future generations

P http://news.bbc.co.uk/go/rss/-/1/hi/scotland/north_east/7535090.stm
每一行都被视为一个单独的字段。对于同一条记录,某些字段出现多次。这是数据模式。正如您所看到的,短语或Q可以在同一条记录中多次出现

其中,行的第一个字母编码:

P:文档的URL T:后时间戳的时间 Q:从文档文本中提取的短语 L:文档中的超链接指向web上的其他文档 如何将此数据集转换为更容易运行聚合、分组、计数、差异和统计的形式


理想情况下,将此数据集转换为csv或json格式最有帮助,这样我就可以使用已经构建的工具,如bash/python/R/mongodb,来进行文本挖掘/处理/自然语言处理

以下代码获取给定格式的文件并生成csv。同一短语的多个外观用管道连接

#!/usr/bin/perl -wl


my @t = qw(P T Q L);
my %h = ();

while (<>) {


    if (/^$/ .. /^$/) {

        my $r = "";

        foreach my $k (@t) {

            $r .= ',' if length($r) > 0;
            $r .= join('|', @{$h{$k}}) if exists($h{$k});           
        }

        print $r;
        %h = ();

    } else {

        if (/^([PTQL])\s+(.*)$/) {

            $h{$1} = () unless exists($h{$1});  
            push @{$h{$1}}, $2;
        }
    }
}

显然,这不是一个Python解决方案,也不是一种简单的方法,因为在字段中无法转义使用的分隔符逗号和管道。

它们为您记录格式。逐行阅读,并在空行处分组转换为JSON。不过,您还有一个更大的问题,因为这些文件往往是4G+。R有内存限制,不管Python ppl怎么说,它也有内存限制。我不确定您将在bash中使用哪些ML/NLP工具。你真的需要考虑你的分析管道/体系结构来做你想做的事情,看起来你陷入了基本的文本文件解析。您建议使用什么来转换为json?在我不必外包给AWS的情况下,您会推荐什么体系结构来处理大型数据集?R、Python、Perl或node或任何东西都可以很容易地完成这种转换。所以这不是一个编码服务,所以除非你把你尝试过但没有成功的东西的代码发布出来,否则没有比这更多的了。我会研究Spark在规模分析中的应用,它有与R&Python的接口。这在任何编程语言中都很容易解决。