繁体   English   中英

python 3.8 如何将 txt 转换为 csv 与 2 个或更多空格分隔符

[英]python 3.8 how to convert txt to csv with seperator of 2 or more spaces

我正在尝试使用 Python 从远程登录 session 转换 txt 导出。 txt看起来像这样

  LocalPort  ChassisId                 PortId PortDescr SysName               

  --------- + ------------------------- ------ --------- ----------------------

  3          01 23 45 67 89 76           1      1         name1

  19         01 23 45 67 89 76           19     19        name2

  21         01 23 45 67 89 76            9      9        name3

  22         01 23 45 67 89 76           22     22        name4

  22         01 23 45 67 89 76           LAN-1  example   name5

  23         01 23 45 67 89 76           LAN-1  example   name6

  23         01 23 45 67 89 76           23     23        name7

所以我用 python 尝试了类似的方法:

read_file = pd.read_csv(OUTPUT_FILE_PROCESSED_TXT, sep="\s{2,}", header=5)
read_file.to_csv(OUTPUT_CSV, sep=",", header=["LocalPort","ChassisId","PortId","PortDescr","SysName"])

但结果是我在生成的 csv 中遗漏了 LocalPort 3 和 LocalPort 22 之间的很大一部分。 这些行被删除。 所以我的 csv 只有标题和最后三行。

谁能帮助我,也许可以解释为什么 Python 正在删除一些行? 我知道其他人问了类似的问题,但解决方案对我没有帮助

提前致谢...

您可以手动解析该OUTPUT_FILE_PROCESSED_TXT文件:

import re

with open(OUTPUT_FILE_PROCESSED_TXT, "r") as f:
    telnet_output = f.read()

rows = telnet_output.splitlines()
values = [re.split(r"\s{2,}", row.strip())  \  # split when >2 spaces
          for row in rows[1:]  \  # avoid header row
          if not row.strip().startswith("-") and row]  # avoid empty and --- rows

columns = rows[0].split()

df = pd.DataFrame(values, columns=columns)

检查您的df是否定义明确:

>>> print(df)

  LocalPort          ChassisId PortId PortDescr SysName
0         3  01 23 45 67 89 76      1         1   name1
1        19  01 23 45 67 89 76     19        19   name2
2        21  01 23 45 67 89 76      9         9   name3
3        22  01 23 45 67 89 76     22        22   name4
4        22  01 23 45 67 89 76  LAN-1   example   name5
5        23  01 23 45 67 89 76  LAN-1   example   name6
6        23  01 23 45 67 89 76     23        23   name7

现在按照您的建议导出到csv

df.to_csv(OUTPUT_CSV, sep=",")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM