Python或Perl将文本报告文件解析到CV

2024-09-23 06:39:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些数据需要解析为制表符分隔的文本文件。数据如下:

>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 Weizen {tab} 47986 {tab} 10325 {tab} ...

有没有人有一些perl代码的例子,我可以用来开始?我对Perl还不熟悉,我在网站上找到了一些其他的例子,但无法让它们在我的上下文中工作。在

我尝试过在Vim中使用正则表达式,还尝试了以下perl:

^{pr2}$

Tags: andofthe数据namesometabreview
1条回答
网友
1楼 · 发布于 2024-09-23 06:39:15

在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";

相关问题 更多 >