简体   繁体   English

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

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

I'm trying to convert a txt-export from a telnet session with Python.我正在尝试使用 Python 从远程登录 session 转换 txt 导出。 The txt looks like this 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

So I tried something like that with python:所以我用 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"])

But the result is that I'm missing a big part between LocalPort 3 and LocalPort 22 in the generated csv.但结果是我在生成的 csv 中遗漏了 LocalPort 3 和 LocalPort 22 之间的很大一部分。 Those lines are removed.这些行被删除。 So my csv is only the headers and the last three lines.所以我的 csv 只有标题和最后三行。

Can anyone help me and maybe explain why Python is removing some lines?谁能帮助我,也许可以解释为什么 Python 正在删除一些行? I know some others asked a similar question but the solutions didn't help me我知道其他人问了类似的问题,但解决方案对我没有帮助

Thanks in advance...提前致谢...

You can parse that OUTPUT_FILE_PROCESSED_TXT file manually:您可以手动解析该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)

Check that your df is well defined:检查您的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

And now export to csv with what you suggested:现在按照您的建议导出到csv

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

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

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