【问题标题】:parsing specific data from .txt file to excel or something else将 .txt 文件中的特定数据解析为 excel 或其他内容
【发布时间】:2013-05-29 08:30:31
【问题描述】:

我已将数据从一个来源提取到 .txt 文件。源是某种地址簿,我使用宏记录器进行提取。现在我有几个完全按照下一种方式格式化的文件(例如 4 个联系人):

Abbrucharbeiten
ATR Armbruster
Werkstr. 28
78727 Oberndorf  
Tel. 0175 7441784
Fax 07423 6280
Abbrucharbeiten
Jensen & Sohn, Karl
Schallenberg 6A
25587 Münsterdorf
Tel. 04821 82538
Fax 04821 83381
Abbrucharbeiten
Kiwitt, R.
Auf der Heide 54
48282 Emsdetten
Tel. 02572 88559
Tel. 0172 7624359
Abbrucharbeiten, Sand und Kies, Transporte, Kiesgruben, Erdbau
Josef Grabmeier GmbH
Reitgesing 1
85560 Ebersberg
Tel. 08092 24701-0
Fax 08092 24701-24

第一行始终是业务的字段(名称) 第二行始终是公司/公司的名称 第三排总是街道地址 第 4 行始终是邮政编码和地点 进而 第 5 行和接下来的几行(有时是两行,有时更多)是 eithar Tel。或传真。

我想对其进行格式化,使其类似于 excel 表,例如:

Branche:    Name:     Address:   Place:    contact1:   contact2:
1st row     2nd row   3rd row    4th row   5th row     6th row.....

现在主要问题是我有超过 500.000 个联系人,我的主要问题是最后一个字段并不总是相同的数字...我不想手动操作,请帮助我...

【问题讨论】:

  • 那么你想写一些代码来做提取吗?
  • 如果有可能用一些代码来解决这个问题而不是肯定的,我对 python 和 vbs 有一些经验,但没有那么多解决这个问题
  • 是的,每个name 都有一个Branche 虽然branche 并不总是相同,在上面的示例中您可以看到,有时它只是一个项目,有时它是几个项目,因为例如,有时只是'Abbrucharbeiten''Transporte',有时是'Abbrucharbeiten, Transporte'

标签: excel parsing delimited


【解决方案1】:

pythonvisual basic 都不是,但翻译成这些语言应该不难。这是perl

perl -lne '
        ## Print header. Either the header and data will be separated with pipes.
        ## Contacts(contact1, contact2, etc) are not included because at this 
        ## moment I can not know how many there will be. It could be done but script
        ## would be far more complex.
        BEGIN { 
                push @header, q|Branche:|, q|Name:|, q|Address:|, q|Place:|;
                printf qq|%s\n|, join q{|}, @header;
        }

        ## Save information for each contact. At least six lines. Over that only
        ## if lines begins with strings "Tel" or "Fax".
        if ( $line < 6 || m/\A(?i)tel|fax/ ) {
                push @contact_info, $_;
                ++$line;

                ## Not skip the printing of last contact.
                next unless eof;
        }

        ## Print info of contact, initialize data structures and repeat process
        ## for the next one.
        printf qq|%s\n|, join q{|}, @contact_info;

        $line = 0;
        undef @contact_info;

        push @contact_info, $_;
        ++$line;

' infile

它是单行的(我知道它看起来不像,但你可以去掉 cmets 并删除换行符来获取它),所以直接从你的 shell 运行它。它产生:

Branche:|Name:|Address:|Place:
Abbrucharbeiten|ATR Armbruster|Werkstr. 28|78727 Oberndorf  |Tel. 0175 7441784|Fax 07423 6280
Abbrucharbeiten|Jensen & Sohn, Karl|Schallenberg 6A|25587 Münsterdorf|Tel. 04821 82538|Fax 04821 83381
Abbrucharbeiten|Kiwitt, R.|Auf der Heide 54|48282 Emsdetten|Tel. 02572 88559|Tel. 0172 7624359
Abbrucharbeiten, Sand und Kies, Transporte, Kiesgruben, Erdbau|Josef Grabmeier GmbH|Reitgesing 1|85560 Ebersberg|Tel. 08092 24701-0|Fax 08092 24701-24

考虑到我没有打印完整的标题并且字段用管道分隔。我认为在 Excel 中导入它是没有问题的。

【讨论】:

  • @pnuts:我不知道你的传真栏是什么意思。据我了解这个问题,一些联系人不能有任何传真,而另一些则有几个。电话也一样。无论如何,这是解决他的问题的开始,他很可能需要对其进行定制以满足他的需求。
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 2016-10-17
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
相关资源
最近更新 更多