我有一个TXT文件,我想导入到Excel中进行研究。但是,在导入之前,我正在努力处理文本格式。这完全是一团糟,屁股你可以看到:
| 1020941333 | 569|SP |500000343 | 9|18.05.2011|15:27:00|18.05.2011|
18.05.2011|Y-0444871-ENCR | 1,93 |BRL |8000800000 |
Juros, Comissões e T | | |
| | | |
|CLB082902 | | | |COEL |COEL |
Y-0444871 |
| 1020941586 | 43|SP |500000344 |43|18.05.2011|15:41:43|18.05.2011|
18.05.2011|B-0447039-ENCR | 9,02 |BRL |8000800000 |
Juros, Comissões e T | | |
| | | |
|CLB082902 | | | |COEL |COEL |
B-0447039 |
| 1021245920 | 956|SP |500000489 | 6|14.06.2011|15:24:02|14.06.2011|
14.06.2011|B-0447039-ENCR | 8,95 |BRL |8000800000 |
Juros, Comissões e T | | |
| | | |
|CLB082902 | | | |COEL |COEL |
B-0447039
|
所以我一直在寻找文字为何如此古怪的答案。我发现这是因为某些CR + LF(回车+换行)放置不正确。我手动进行了一些更正,通过它我可以看到可以更好地组织文本,如下所示:
--------------------------------------------------------------------------------
| Nº documento | LL.|TpDoc.|Nº doc.ref|LL|Entrado em|Hora |Data doc. |Dt.lçto. |Elemento PEP | Valor/moeda ACC|MdACC|Cl.custo |Denom.classe custo |Material | Qtd.entr.|Texto breve material |UML |Doc.compra| Item|Texto do pedido |Usuário |DEs |Est |Nº ref.estorno |Empr. |EmFI |Definição do projeto
--------------------------------------------------------------------------------
| 1016939462 | 1|WE |5000058364| 1|22.02.2010|10:52:43|22.02.2010|22.02.2010|Y0444871PROJELMC | 540,93 |BRL |8000124000 |Serviço de Terceiro | | 1,000 | |UR |4501328844| 1|ESTUDOS E PROJ. REDE |CLB055760 | | | |COEL |COEL |Y-0444871 |
| 1020016002 | 1|WE |5000053667| 1|15.02.2011|11:56:05|15.02.2011|15.02.2011|B0447039PROJELMC | 2.011,84 |BRL |8000124000 |Serviço de Terceiro | | 1,000 | |UR |4501633481| 1|ESTUDOS E PROJ. REDE |CLB093440 | | | |COEL |COEL |B-0447039 |
| 1020258918 | 798|SP |500000121 | 8|15.03.2011|18:06:18|15.03.2011|15.03.2011|B-0447039-ENCR | 6,92 |BRL |8000800000 |Juros, Comissões e T | | | | | | | |CLB107395 | | | |COEL |COEL |B-0447039 |
| 1020585116 | 761|SP |500000225 | 1|15.04.2011|14:13:44|15.04.2011|15.04.2011|Y-0444871-ENCR | 1,88 |BRL |8000800000 |Juros, Comissões e T | | | | | | | |CLB145327 | | | |COEL |COEL |Y-0444871 |
| 1020586939 | 184|SP |500000230 | 4|15.04.2011|16:22:41|15.04.2011|15.04.2011|B-0447039-ENCR | 7,03 |BRL |8000800000 |Juros, Comissões e T | | | | | | | |CLB145327 | | | |COEL |COEL |B-0447039 |
我也可以在文本中看到一个模式。每行以该字符|
开头。因此,对于每一个不以“ |”开头的行,都应与前一行合并。
问题原状:
| 1020941333 | 569|SP |500000343 | 9|18.05.2011|15:27:00|18.05.2011|
18.05.2011|Y-0444871-ENCR | 1,93 |BRL |8000800000 |
Juros, Comissões e T | | |
| | | |
|CLB082902 | | | |COEL |COEL |
Y-0444871 |
| 1020941586 | 43|SP |500000344 |43|18.05.2011|15:41:43|18.05.2011|
18.05.2011|B-0447039-ENCR | 9,02 |BRL |8000800000 |
Juros, Comissões e T | | |
| | | |
|CLB082902 | | | |COEL |COEL |
B-0447039 |
所需的输出
| 1020941333 | 569|SP |500000343 | 9|18.05.2011|15:27:00|18.05.2011|18.05.2011|Y-0444871-ENCR | 1,93 |BRL |8000800000 |Juros, Comissões e T | | | | | | | |CLB082902 | | | |COEL |COEL |Y-0444871 |
| 1020941586 | 43|SP |500000344 |43|18.05.2011|15:41:43|18.05.2011|18.05.2011|B-0447039-ENCR | 9,02 |BRL |8000800000 |Juros, Comissões e T | | | | | | | |CLB082902 | | | |COEL |COEL |B-0447039 |
在Notepad ++中实现它有很多困难。我无法手动执行此操作,因为该文件有490万行。如果有人可以使用Notepad ++或其他可以更好地解决此问题的软件向我展示一些有关此问题的信息,我将非常感激。
答案 0 :(得分:1)
您可以使用正则表达式查找在换行符之后的管道,并使用负号(?!
来检查管道右侧的内容是否不是开始新行的模式。然后替换为第一个捕获组以保留管道。
查找内容:
(\|)\R(?!\|[ \t]+\d+[ \t]+\|)
替换为:
$1
说明
(\|)
匹配捕获组中的管道\R
匹配unicode换行符序列(?!
负向超前
\|[ \t]+\d+[ \t]+\|
匹配管道,空格或制表符1+倍,数字1+,空格和制表符1+以及管道)
近距离否定预测请参见regex demo
答案 1 :(得分:1)
这将替换任何类型的换行符,然后再换成不带任何字符的管道:
\R(?!\|)
LEAVE EMPTY
说明:
\R # any kind of linebreak (ie. \r, \n, \r\n)
(?! # negative lookahead, zero length assertion that makes sure we do not have after:
\| # a pipe character
) # end lookahead
给定示例的结果
| 1020941333 | 569|SP |500000343 | 9|18.05.2011|15:27:00|18.05.2011|18.05.2011|Y-0444871-ENCR | 1,93 |BRL |8000800000 |Juros, Comissões e T | | | | | | | |CLB082902 | | | |COEL |COEL |Y-0444871 |
| 1020941586 | 43|SP |500000344 |43|18.05.2011|15:41:43|18.05.2011|18.05.2011|B-0447039-ENCR | 9,02 |BRL |8000800000 |Juros, Comissões e T | | | | | | | |CLB082902 | | | |COEL |COEL |B-0447039 |