2013-10-19 83 views
0

我有一些数据需要解析为制表符分隔的文本文件。数据是这样的:Python或Perl解析CVS文本报告文件

>beer/name: Sausa Weizen beer/beerId: 47986 beer/brewerId: 10325 beer/ABV: 5.00 beer/style: Hefeweizen review/appearance: 2.5 
> review/aroma: 2 review/palate: 1.5 review/taste: 1.5 review/overall: 
> 1.5 review/time: 1234817823 review/profileName: stcules review/text: A lot of foam. But a lot. In the smell some banana, and then lactic and 
> tart. Not a good start. Quite dark orange in color, with a lively 
> carbonation (now visible, under the foam). Again tending to lactic 
> sourness. Same for the taste. With some yeast and banana.  
> 
> beer/name: Red Moon ...repeats millions of times... 

` 我需要它看起来像这样:

Sausa小麦啤酒{标签} {47986}标签{10325}标签...

有没有人有我可以用来开始的一些示例Perl代码?我是Perl的新手,我在网站上发现了一些其他的例子,但却无法让它们在我的上下文中运行。

我使用Vim的正则表达式,也是perl支持以下尝试:

#!/usr/bin/perl 
#parse_file_kv.pl 
use strict; 
use warnings; 
my $save_input_record_separator = $/; #Save original value before changing it 
undef $/; # enable slurp mode 
open(my $file ,"ratebeer.txt"); 
$/ = $save_input_record_separator; #Restore original value to this global variable 
my %h = $file =~ m/\w+/g;#Read keys and values from file into hash %h 
for (keys %h){ 
    print "KeyWord $_ has value $h{$_}.\n"; 
} 
print "\n"; 
my @kws2find = qw(beer/name); 
foreach (@kws2find){ 
    find_value($_); 
} 
sub find_value{ 
    my $kw = shift @_; 
    if (exists $h{$kw}){ 
     print "Value of $kw is $h{$kw}\n"; 
    }else{ 
     print "Keyword $kw is not found in hash\n"; 
    } 
} 
+0

你能证明你试过的吗? – Andy

回答

0

在Perl中,有很多方法可以做到这一点,但我会给予最起码的:

# a sample input line. In reality you would read it from a file and chomp off the \n. 
my $foo = "beer/name: Sausa Weizen beer/beerId: 47986 ...\n"; 

# replace foo/bar: with a tab everywhere in the line. 
# I used A-Za-z as the chars to match, you can do many more things (including more 
# elegant ways of specifying whole character classes). 
# 
$foo =~ s/[A-Za-z]*\/[a-zA-Z]*:/\t/g; 

# print it out. 
print "$foo\n";