如何使用正则表达式和/或Python将此专有文本文件解析为CSV?

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

我从旧的专有系统中获得了一个配置文件,我想将其解析为 CSV,以便我可以将其导入 SharePoint 列表或 Excel 文件,并使用 Power BI 或 Power Apps 访问它。 (旧系统可能叫ptube,但我不确定。)

部分文件如下。有人告诉我,真正需要导出的是每个项目的第一个条目(在本摘录中为“Zone1”、“Blower100”和“TU110”)和字段“cycles” - 所有其他数据都可以被丢弃。一些“项目”没有“周期”字段 - 这些可以完全忽略。以

//
开头的行显然是注释,可以忽略。

因此,对于下面的文件,我们想要:

Blower100, 2693452
TU110, 577754

输出到文本或Excel文件。 (请注意,第一个条目“Zone1”被跳过,因为该项目没有“cycles”字段。)

该文件包含48K行,下面的文件中只有前三项。他们希望能够定期重新加载此文件,因此我需要某种可以定期运行的脚本来执行此操作。

我的编程背景主要是 Java 和 SQL,我可能可以使用 Java 来完成此任务,但在这项新工作中我可用的工具有限,而且无法访问 Java。我在这里的工作是开发 Power Apps 和 Power BI 报告以及 Power Automate 脚本,所有这些我都非常熟悉,但我不知道如何仅使用这些工具来转换此文件。

我拥有 Windows 10 中可用的所有内容(包括 PowerShell,但不包括 cmd),以及下面列出的软件。公司给我的笔记本电脑非常“锁定”,我无法安装额外的软件,但如果我能证明其合理性,我可以要求 IT 安装软件(而且它很便宜或免费)。

我可用的软件是:

  • Power Platform 套件(Power Apps、Power BI、Power Automate)
  • 我可以创建 SharePoint 列表
  • 所有 Office 365 应用程序(例如 Excel)
  • SQL,以及对 SQL Server 和 SSMS 的访问
  • 带有 Python 的 Visual Studio 2022(我不知道,但希望 学习)
  • PowerShell(但不是 CMD)

我知道这是一个很多问题,但如果有人可以帮助我,或者给我指出解决方案,我将不胜感激!

格里芬

// Server Version: 7.2.5.1 [May 28 2020 05:59:27]
// ESP Version: 7.02.05.01 12:00:00 [ 09/07/22 ]
// Configuration saved: Fri Nov 17 13:46:52
Zone1 {
  aliasName= "";
  unitDescriptor= "";
  retries= 0;
  totalRetries= 0;
  resetRetriesTm= 0;
  successMoves= 0;
  recvrySet= ;
  recovery= Station220;
  useZeroLevel= 0;
  outsideAir= 0;
  commLine= 255;
  fullTime= 0;
  speed= 20;
  // Equipment level configuration
  port 0 : Blower100;
}

Blower100 {
  aliasName= "";
  unitDescriptor= "Core Penthouse";
  retries= 0;
  totalRetries= 0;
  resetRetriesTm= 1686697175;
  successMoves= 0;
  isXpBlower= 0;
  commLine= Line410;
  strapAdd= 100;
  pvTime= 3;
  idleTime= 10;
  purgeTime= 15;
  mechanics= 0;
  cycles= 2693452;
  offOn= 0;
  starts= 2123;
  startTm= 1700243194;
  resetStart= 1658292126;
  hours= 38173707;
  resetHrs= 1658292122;
  resetCycleTm= 1658292118;
  revision= "03.00.00.007";
  csum= " ";
  promDate= "11/25/13";
  program= "TUBLOWER.BN3";
  downloading= 0;
  bootloadertype= 1;
  // Equipment level configuration
  port 0 : TU110;
  up : Zone1;
}

TU110 {
  aliasName= "";
  unitDescriptor= "Core Penthouse.  Above platform";
  retries= 3;
  totalRetries= 1;
  resetRetriesTm= 1686697188;
  successMoves= 0;
  revision= "03.00.00.007";
  csum= " ";
  promDate= "11/25/13";
  restrictTrnarnd= 0;
  commLine= Line412;
  strapAdd= 110;
  upStreamPort= 0;
  ports= 4;
  mechanics= 0;
  cycles= 577754;
  resetCycleTm= 1634092201;
  costs[0]= 1;
  costs[1]= 1;
  costs[2]= 1;
  costs[3]= 1;
  costs[4]= 1;
  costs[5]= 1;
  samples[0]= 10;
  samples[1]= 0;
  samples[2]= 0;
  samples[3]= 0;
  samples[4]= 0;
  samples[5]= 0;
  program= "TUBLOWER.BN3";
  downloading= 0;
  bootloadertype= 1;
  // Equipment level configuration
  port 0 : TU101;
  port 1 : ByPass971;
  port 2 : IZ930;
  port 3 : IZ931;
  up : Blower100;
}

python csv parsing text
1个回答
0
投票

这是我的方法

  1. 扫描输入文件中的行
  2. 如果该行以左大括号结束
    {
    ,请记录之前的文本
  3. 如果该行包含“cycles=”,则记录等号后面的值
  4. 最后,数据被收集到
    rows
    变量中并写入文件
import csv

rows = []
col0 = ""
col1 = ""

with open("in.txt") as stream:
    for line in stream:
        line = line.strip()
        if line.endswith("{"):
            col0 = line.split()[0]
        elif "cycles" in line and "=" in line:
            col1 = line.removesuffix(";").split("=", 1)[-1]
            rows.append([col0, col1])

with open("out.csv", "w") as stream:
    writer = csv.writer(stream)
    writer.writerows(rows)
© www.soinside.com 2019 - 2024. All rights reserved.