我无法使用 python 有效地将数据与此文本分离(不同字符“:,|”)

问题描述 投票:0回答:0

我有一个带有表的 txt,其中包含文本变量中的内容,我想将元素分成列(AREA、NUMBER、VALUE、VALIDITY、ZONE),如果区域和区域重复则无关紧要

enter image description here

texto = 'AREA : RURAL,          VIGENCIA :2013/01/01,   CODIGO : 54001000101, 1 | 14.700.000 |  2 | 10.800.000 |  3 |  9.900.000 | ZONAS :  01 02 03 '

AREA = texto.split(":")[1].split(",")[0]
VIGENCIA = texto.split(":")[2].split(",")[0]
CODIGO = texto.split(":")[3].split("|")[0]
ZONAS = texto.split(":")[4].split("-")[0]

for i in range(0, len(texto.split('|')), 2):
    col1 = texto.split('|')[i+1]
    col2 = texto.split('|')[i+2]

    print(AREA,"|",col1,"|",col2,"|",VIGENCIA,"|",ZONAS)

当我运行它时出现此错误: error showing

我的代码输出我想要的文本(但无序),我可以用分隔符 (|) 在 excel 中拆分它,但是这段代码可以在 csv 中给我分隔的列吗?

我可以创建一个直接从文本中获取文本而无需复制和粘贴的文本吗?

enter image description here

非常感谢社区,这是我的一个小个人项目,它可能比你想象的要容易

python extract text-extraction
© www.soinside.com 2019 - 2024. All rights reserved.